-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
282 lines (222 loc) · 6.84 KB
/
Copy pathbot.js
File metadata and controls
282 lines (222 loc) · 6.84 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Creating telegram bot and adding twitter interacting
// importing modules
const Telebot = require('telebot');
const twit = require('twit');
const fetch = require('node-fetch');
const fs = require('fs');
// Instantiating Telegram bot with API key provided // IMPORTANT, PLEASE MAKE CHANGES
const bot = new Telebot({
token: 'Telegram Bot Token Here', // IMPORTANT, PLEASE MAKE CHANGES
usePlugins: ['commandButton', 'askUser']
});
// Saving Twitter configutaions to a const // IMPORTANT, PLEASE MAKE CHANGES
const tConfig = {
consumer_key: 'Twitter Consumer Key Here',
consumer_secret: 'Twitter Consumer Secret Here',
access_token: 'Twitter Access Token Here',
access_token_secret: 'Twitter Token Secret Here'
} // IMPORTANT, PLEASE MAKE CHANGES
// Instantiating Twit module with required twitter configurations
const T = new twit(tConfig);
// Twitter Methods Begin
// Method to Post a Twit
sendTwit = (msg) => {
let id = msg.from.id;
let replyToMessage = msg.message_id;
let params = {
status: `${msg.text}`
}
console.log(params.status);
T.post('statuses/update', params, function (err, data, response) {
if (data) {
bot.sendMessage(
id, "Sent Successfully", {
replyToMessage
}
);
}
if (err) {
bot.sendMessage(id, "Sorry! error occured", {
replyToMessage
})
}
})
}
// Method to get own Timeline
ownTimeline = (msg) => {
T.get('statuses/home_timeline', {
count: 15,
truncated: false,
tweet_mode: 'extended'
}, (err, data, response) => {
data.forEach(element => {
bot.sendMessage(msg.from.id, element.full_text);
});
});
}
// Method to get another users's timeline
userTimeline = (msg) => {
let id = msg.from.id;
let replyToMessage = msg.message_id;
T.get('statuses/user_timeline', {
screen_name: `${msg.text}`,
count: 15,
tweet_mode: 'extended'
}, (err, data, response) => {
if (data[0].full_text != undefined) {
for(let i=0;i<2;i++){
if (i==0) {
bot.sendMessage(id, `Tweets from ${msg.text}`);
}else if(i==1){
data.forEach(element => {
bot.sendMessage(id, element.full_text);
});
}
}
} else {
bot.sendMessage(id, "Sorry can't find User", {
replyToMessage
})
}
if (err) {
bot.sendMessage(id, "Sorry can't find User", {
replyToMessage
})
}
});
}
tweetWithImage = (msg) => {
// Method post a tweet with media
let status = msg.caption;
let id = msg.from.id;
let replyToMessage = msg.message_id;
let b64content = fs.readFileSync('./2.png', {
encoding: 'base64'
})
console.log("Status:", status);
if (status == undefined) {
status = "."
}
// first we must post the media to Twitter
T.post('media/upload', {
media_data: b64content
}, function (err, data, response) {
// now we can assign alt text to the media, for use by screen readers and
// other text-based presentations and interpreters
let mediaIdStr = data.media_id_string
let meta_params = {
media_id: mediaIdStr,
}
T.post('media/metadata/create', meta_params, function (err, data, response) {
if (!err) {
// now we can reference the media and post a tweet (media will attach to the tweet)
let params = {
status: status,
media_ids: [mediaIdStr]
}
T.post('statuses/update', params, function (err, data, response) {
if (data) {
bot.sendMessage(
id, "Sent Successfully", {
replyToMessage
} );
console.log(data);
}
if (err) {
bot.sendMessage(id, "Sorry! error occured", {
replyToMessage
})
}
})
}
})
})
}
// Twitter Methods End
// Metohd to save file on disk from telegram server
async function saveFile(info, msg) {
const res = fetch(info.fileLink)
const result = await res;
const dest = await fs.createWriteStream('./2.png');
const save = await result.body.pipe(dest);
return new Promise((resolve, reject) => {
dest.on('finish', resolve)
dest.on('error', reject)
})
}
// Telegram Bot Setup starts
// Command /twitter
bot.on('/twitter', (msg) => {
// Inline keyboard markup
const replyMarkup = bot.inlineKeyboard([
[
// First row with command callback button
bot.inlineButton('Get own timeline', {
callback: '/getOwn'
})
],
[
// Second row with command callback button
bot.inlineButton('Get user tweets', {
callback: '/getUser'
})
],
[
// Third row with command callback button
bot.inlineButton('Send Tweet', {
callback: '/sendTweet'
})
],
]);
// Send message with keyboard markup
return bot.sendMessage(msg.from.id, 'Choose Option', {
replyMarkup
});
});
// Command /getOwn
bot.on('/getOwn', msg => {
return ownTimeline(msg);
});
// Command /getUser
bot.on('/getUser', msg => {
const id = msg.from.id;
return bot.sendMessage(id, 'Enter twitter username:', {
ask: 'username'
});
});
// Command /status
bot.on('/sendTweet', msg => {
const id = msg.from.id;
return bot.sendMessage(id, 'Enter status:', {
ask: 'status'
});
});
// Button callback
bot.on('callbackQuery', (msg) => {
bot.answerCallbackQuery(msg.id);
});
// Addding buttons for Twitter interactivity
// Asking events
// Ask twitter username event
bot.on('ask.username', msg => {
const id = msg.from.id;
return userTimeline(msg);
});
// Ask status for twitter event
bot.on('ask.status', (msg, self) => {
let id = msg.from.id;
let replyToMessage = msg.message_id;
let type = self.type;
let parseMode = 'html';
if (self.type == "photo") {
return fileinfo =
bot.getFile(msg.photo[(msg.photo).length - 1].file_id)
.then(async (info) => {
await saveFile(info, msg);
await tweetWithImage(msg);
});
} else if (self.type == "text") {
return sendTwit(msg);
}
});
bot.start();