-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram.coffee
More file actions
105 lines (85 loc) · 3.03 KB
/
telegram.coffee
File metadata and controls
105 lines (85 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{Robot, Adapter, TextMessage, User} = require 'hubot'
request = require 'request'
class Telegram extends Adapter
constructor: ->
super
@robot.logger.info "Telegram Adapter loaded"
@token = process.env['TELEGRAM_TOKEN']
@webHook = process.env['TELEGRAM_WEBHOOK']
@api_url = "https://api.telegram.org/bot#{@token}"
@interval = +process.env['TELEGRAM_INTERVAL'] or 3000
@offset = 0
# Get the Bot Id and name...not used by now
request "#{@api_url}/getMe", (err, res, body) =>
@id = JSON.parse(body).result.id if res.statusCode == 200
send: (envelope, strings...) ->
data =
url: "#{@api_url}/sendMessage"
form:
chat_id: envelope.room
text: strings.join()
disable_web_page_preview: true
request.post data, (err, res, body) =>
@robot.logger.info res.statusCode
reply: (envelope, strings...) ->
data =
url: "#{@api_url}/sendMessage"
form:
chat_id: envelope.room
text: strings.join()
request.post data, (err, res, body) =>
@robot.logger.info res.statusCode
receiveMsg: (msg) ->
text = msg.message?.text
# Only if it's a text message, not join or leaving events
if text
# Strip any commands and mentions
if text[0] is '/' or text[0] is '@'
text = text.slice(text.indexOf(' ')+1)
## If is a direct message to the bot, prepend the name
##text = @robot.name + ' ' + msg.message.text if msg.message.chat.id > 0
text = @robot.name + ' ' + text
user = @robot.brain.userForId msg.message.from.id, name: msg.message.from.username, room: msg.message.chat.id
message = new TextMessage user, text, msg.message_id
@receive message
@offset = msg.update_id
getLastOffset: ->
# Increment the last offset
parseInt(@offset) + 1
run: ->
self = @
@robot.logger.info "Run"
unless @token
@emit 'error', new Error `'The environment variable \`\033[31mTELEGRAM_TOKEN\033[39m\` is required.'`
if @webHook
# Call `setWebHook` to dynamically set the URL
data =
url: "#{@api_url}/setWebHook"
form:
url: @webHook
request.post data, (err, res, body) =>
@robot.logger.info res.statusCode
@robot.router.post "/telegram/receive", (req, res) =>
console.log req.body
for msg in req.body.result
@robot.logger.info "WebHook"
@receiveMsg msg
else
setInterval ->
url = "#{self.api_url}/getUpdates?offset=#{self.getLastOffset()}"
self.robot.http(url).get() (err, res, body) ->
self.emit 'error', new Error err if err
try
updates = JSON.parse body
catch ex
console.log("#{ex.message}\nWhile parsing:\n#{body}")
return
unless updates.ok
console.log("#{updates.description} (#{updates.error_code})")
return
for msg in updates.result
self.receiveMsg msg
, @interval
@emit "connected"
exports.use = (robot) ->
new Telegram robot