forked from nodegin/tglib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTG.js
64 lines (61 loc) · 1.56 KB
/
TG.js
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
// combine the user-supplied options with the referencing base options
function combine(object, options) {
Object.keys(object).forEach((key) => {
if (object[key] instanceof Object) {
combine(object[key], options)
} else if (options[key] !== undefined) {
object[key] = options[key]
}
})
}
class TG {
constructor(client) {
this.client = client
}
/*
* Sends text message to a existing chat.
*/
async sendTextMessage(chatId, text, options = {}) {
let formattedText
switch (options.parse_mode) {
case 'html':
formattedText = await this.client._execute({
'@type': 'parseTextEntities',
'parse_mode': { '@type': 'textParseModeHTML' },
text,
})
break
case 'markdown':
formattedText = await this.client._execute({
'@type': 'parseTextEntities',
'parse_mode': { '@type': 'textParseModeMarkdown' },
text,
})
break
default: {
formattedText = {
'@type': 'formattedText',
text,
}
break
}
}
const payload = {
'@type': 'sendMessage',
'chat_id': chatId,
'reply_to_message_id': 0,
'disable_notification': false,
'from_background': true,
'reply_markup': null,
'input_message_content': {
'@type': 'inputMessageText',
'text': formattedText,
'disable_web_page_preview': true,
'clear_draft': true,
},
}
combine(payload, options)
return this.client.fetch(payload)
}
}
module.exports = TG