 
   constructor: ->
     super
-    @robot.logger.info "Telegram Adapter"
+    @robot.logger.info "Telegram Adapter loaded"
 
     @token = process.env['TELEGRAM_TOKEN']
     @api_url = "https://api.telegram.org/bot#{@token}"
     
-    @webHook = null
-    @lastMessage = 1
+    @webHook = process.env['TELEGRAM_WEBHOOK']
+    @lastUpdateId = 0
     
   send: (envelope, strings...) ->
-    self = @
-    @robot.logger.info "Send"
-    
-    console.log envelope
-    console.log strings
-    
+   
     reply = 
       chat_id: envelope.room
       text: strings.join()
     
-    console.log reply
-    
-    request.post { url: "#{@api_url}/sendMessage", form: reply }, (err, httpResponse, body) ->
-      self.robot.logger.info body
+    request.post { url: "#{@api_url}/sendMessage", form: reply }, (err, httpResponse, body) =>
+      @robot.logger.info httpResponse.statusCode
 
   reply: (envelope, strings...) ->
     @robot.logger.info "Reply"
 
   receiveMsg: (msg) ->
-    console.log "Receiving message!"
     user = @robot.brain.userForId msg.message.from.id, name: msg.message.from.username, room: msg.message.chat.id
-    # user = new User msg.message.from.id, name: msg.message.from.username, room: msg.message.chat.id
     message = new TextMessage user, msg.message.text, msg.message_id
     @receive message
-    @lastMessage = msg.update_id
+    @lastUpdateId = msg.update_id
+
+  getLastUpdateId: ->
+    parseInt(@lastUpdateId) + 1
     
   run: ->
     self = @

# {Robot, Adapter, TextMessage, EnterMessage, LeaveMessage, TopicMessage} = require 'hubot'
{Robot, Adapter, TextMessage, User} = require 'hubot'
request = require 'request'


      
exports.use = (robot) ->
  new Telegram robot
