forked from manuchekhr32/send-telegram-message-with-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
186 lines (170 loc) · 7.75 KB
/
app.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
class TelegramBotSetup {
constructor(token) {
this.token = token;
this.requestUrl = 'https://api.telegram.org/bot';
}
api(type, method, body) {
return new Promise((resolve, reject) => {
fetch(this.requestUrl + this.token + type, {
method: method,
body: body
}).then(res => {
resolve(res.json())
}).catch(err => {
reject(err)
})
})
}
}
class Bot extends TelegramBotSetup {
constructor(botToken, defaultChatID) {
super(botToken);
this.dcid = defaultChatID;
}
static start() {
console.log("Send telegram message with JS\nDeveloper: https://manuchehr.me\nDocs: https://github.com/manuchekhr32/send-telegram-message-with-js");
}
async getUpdates() {
try {
const result = await this.api('/getUpdates', 'GET')
return await result
} catch(e) {
return await e
}
}
async getMe() {
try {
const result = await this.api('/getMe', 'GET')
return await result
} catch(e) {
return await e
}
}
async sendMessage(text, chatID, parseMode, disableNotification) {
try {
const result = await this.api(`/sendMessage?text=${text}&chat_id=${chatID ? chatID : this.dcid}&parse_mode=${parseMode ? parseMode : 'html'}&disable_notification=${disableNotification ? disableNotification : false}`, 'GET')
return await result
} catch(e) {
return await e
}
}
async sendPhoto(img, caption, chatID, parseMode, disableNotification) {
try {
if (img.startsWith('#')) {
const file = document.getElementById(img.replace('#', ''));
const formData = new FormData();
formData.append("photo", file.files[0])
const result = await this.api(`/sendPhoto?caption=${caption ? caption : ''}&chat_id=${chatID ? chatID : this.dcid}&parse_mode=${parseMode ? parseMode : 'html'}&disable_notification=${disableNotification ? disableNotification : false}`,
'POST', formData)
return await result
}
else if (typeof img === 'string') {
const result = await this.api(`/sendPhoto?photo=${img}&caption=${caption ? caption : ''}&chat_id=${chatID ? chatID : this.dcid}&parse_mode=${parseMode ? parseMode : 'html'}&disable_notification=${disableNotification ? disableNotification : false}`, 'GET')
return await result
}
} catch(e) {
return await e
}
}
async sendAudio(audio, caption, chatID, parseMode, disableNotification) {
try {
if (audio.startsWith('#')) {
const file = document.getElementById(audio.replace('#', ''));
const formData = new FormData();
formData.append("audio", file.files[0])
const result = await this.api(`/sendAudio?caption=${caption ? caption : ''}&chat_id=${chatID ? chatID : this.dcid}&parse_mode=${parseMode ? parseMode : 'html'}&disable_notification=${disableNotification ? disableNotification : false}`,
'POST', formData)
return await result
}
else if (typeof audio === 'string') {
const result = await this.api(`/sendAudio?audio=${audio}&caption=${caption ? caption : ''}&chat_id=${chatID ? chatID : this.dcid}&parse_mode=${parseMode ? parseMode : 'html'}&disable_notification=${disableNotification ? disableNotification : false}`, 'GET')
return await result
}
} catch(e) {
return await e
}
}
async sendVoice(voice, caption, chatID, parseMode, disableNotification) {
try {
if (voice.startsWith('#')) {
const file = document.getElementById(voice.replace('#', ''));
const formData = new FormData();
formData.append("voice", file.files[0])
const result = await this.api(`/sendVoice?caption=${caption ? caption : ''}&chat_id=${chatID ? chatID : this.dcid}&parse_mode=${parseMode ? parseMode : 'html'}&disable_notification=${disableNotification ? disableNotification : false}`,
'POST', formData)
return await result
}
else if (typeof voice === 'string') {
const result = await this.api(`/sendVoice?voice=${voice}&caption=${caption ? caption : ''}&chat_id=${chatID ? chatID : this.dcid}&parse_mode=${parseMode ? parseMode : 'html'}&disable_notification=${disableNotification ? disableNotification : false}`, 'GET')
return await result
}
} catch(e) {
return await e
}
}
async sendVideo(video, caption, chatID, parseMode, disableNotification) {
try {
if (video.startsWith('#')) {
const file = document.getElementById(video.replace('#', ''));
const formData = new FormData();
formData.append("video", file.files[0])
const result = await this.api(`/sendVideo?caption=${caption ? caption : ''}&chat_id=${chatID ? chatID : this.dcid}&parse_mode=${parseMode ? parseMode : 'html'}&disable_notification=${disableNotification ? disableNotification : false}`,
'POST', formData)
return await result
}
else if (typeof video === 'string') {
const result = await this.api(`/sendVideo?video=${video}&caption=${caption ? caption : ''}&chat_id=${chatID ? chatID : this.dcid}&parse_mode=${parseMode ? parseMode : 'html'}&disable_notification=${disableNotification ? disableNotification : false}`, 'GET')
return await result
}
} catch(e) {
return await e
}
}
async sendFile(doc, caption, chatID, parseMode, disableNotification) {
try {
if (doc.startsWith('#')) {
const file = document.getElementById(doc.replace('#', ''));
const formData = new FormData();
formData.append("document", file.files[0])
const result = await this.api(`/sendDocument?caption=${caption ? caption : ''}&chat_id=${chatID ? chatID : this.dcid}&parse_mode=${parseMode ? parseMode : 'html'}&disable_notification=${disableNotification ? disableNotification : false}`,
'POST', formData)
return await result
}
else if (typeof doc === 'string') {
const result = await this.api(`/sendDocument?document=${doc}&caption=${caption ? caption : ''}&chat_id=${chatID ? chatID : this.dcid}&parse_mode=${parseMode ? parseMode : 'html'}&disable_notification=${disableNotification ? disableNotification : false}`, 'GET')
return await result
}
} catch(e) {
return await e
}
}
async sendContact(phoneNumber, firstName, lastName, chatID, parseMode, disableNotification) {
try {
const result = await this.api(`/sendContact?phone_number=${phoneNumber}&first_name=${firstName}&last_name=${lastName ? lastName : ''}&chat_id=${chatID ? chatID : this.dcid}&parse_mode=${parseMode ? parseMode : 'html'}&disable_notification=${disableNotification ? disableNotification : false}`, 'GET')
return await result
} catch(e) {
return await e
}
}
async sendLocation(latitude, longitude, chatID, parseMode, disableNotification) {
try {
const result = await this.api(`/sendLocation?latitude=${latitude}&longitude=${longitude}&chat_id=${chatID ? chatID : this.dcid}&parse_mode=${parseMode ? parseMode : 'html'}&disable_notification=${disableNotification ? disableNotification : false}`, 'GET')
return await result
} catch(e) {
return await e
}
}
async sendPoll(question, options, config, chatID, disableNotification) {
try {
let url = `/sendPoll?question=${question}&options=${JSON.stringify(options)}&chat_id=${chatID ? chatID : this.dcid}&disable_notification=${disableNotification ? disableNotification : false}`
for (let i in config) {
url += `&${i}=${config[i]}`
}
const result = await this.api(url, "GET")
return await result
} catch(e) {
return await e
}
}
}
Bot.start()