       
 exports.use = (robot) ->
   new Telegram robot
-  
# {Robot, Adapter, TextMessage, EnterMessage, LeaveMessage, TopicMessage} = require 'hubot'
{Robot, Adapter, TextMessage, User} = require 'hubot'
request = require 'request'



  constructor: ->
    super
    @robot.logger.info "Telegram Adapter loaded"

    @token = process.env['TELEGRAM_TOKEN']
    @api_url = "https://api.telegram.org/bot#{@token}"
    
    @webHook = process.env['TELEGRAM_WEBHOOK']
    @lastUpdateId = 0
    
  send: (envelope, strings...) ->
   
    reply = 
      chat_id: envelope.room
      text: strings.join()
    
    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) ->
    user = @robot.brain.userForId 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
    @lastUpdateId = msg.update_id

  getLastUpdateId: ->
    parseInt(@lastUpdateId) + 1
    
  run: ->
    self = @
