-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
112 lines (99 loc) · 2.66 KB
/
bot.js
File metadata and controls
112 lines (99 loc) · 2.66 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
106
107
108
109
110
111
112
/**
* @file Exports the Bot class
*/
const request = require('request-promise')
const logger = require('./logger')
const caption = {
"yes": "Yes",
"no": "No",
"again": "Try asking again",
"dont": "I don't think so",
"maybe": "Maybe someday",
"greetings": "Greetings"
}
/**
* The Bot object interacts with the Telegram API.
* @class
* @requires module:request/request-promise
*/
class Bot {
/**
* Creates an instance of Bot.
* @param {string} url - The API address to which this Bot will send requests
*/
constructor(url) {
this.api = url
}
/**
* Requests additional data about this Bot.
* @returns {Promise}
*/
init() {
return request(`${this.api}/getMe`)
.then(body => {
if ('string' === typeof body) body = JSON.parse(body)
if (body.ok) {
this.id = body.result.id
this.name = body.result.first_name
this.username = body.result.username
}
})
.catch(err => logger.error(err.stack))
}
/**
* Sends a sound file to chat.
* @param {string} chat_id - Chat identifier
* @param {string|Audio} audio - Audio file or identifier of existing file
* @param {string} [reply_id] - User identifier to address reply
* @param {string} [type] - Answer type for captioning
*/
sendAudio(chat_id, audio, reply_id, type) {
const formType = (typeof audio == 'string') ? 'form' : 'formData'
let options = {
'url': `${this.api}/sendVoice`,
[formType]: {
'chat_id': chat_id,
'disable_notification': true,
'voice': audio
}
}
if (reply_id) {
options[formType] = Object.assign(options[formType], {'reply_to_message_id': reply_id})
}
if (type && caption[type]) {
options[formType] = Object.assign(options[formType], {'caption': caption[type]})
}
return request.post(options)
}
/**
* Sends a text message to chat.
* @param {string} chat_id - Chat identifier
* @param {string} text - Message to send
* @param {string} [reply_id] - User identifier to address reply
*/
sendMessage(chat_id, text, reply_id) {
return request.post({
'url': `${this.api}/sendMessage`,
'form': {
'chat_id': chat_id,
'text': text,
'parse_mode': 'Markdown',
'reply_to_message_id': reply_id || null
}
})
}
/**
* Sets an HTTP address to receive messages addressed to this Bot.
* @param {string} server - HTTP address of the Bot
*/
setWebhook(server) {
return request.post({
'url': `${this.api}/setWebhook`,
'form': {
'url': server,
'allowed_updates': ['message']
}
})
}
}
module.exports = Bot