-
Notifications
You must be signed in to change notification settings - Fork 1
/
retwitgram.py
385 lines (343 loc) · 15.2 KB
/
retwitgram.py
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import tweepy
import re
from uuid import uuid4
from telegram.ext import Updater, CommandHandler, MessageHandler, \
Filters, InlineQueryHandler, CallbackQueryHandler
from telegram import InlineQueryResultArticle, ParseMode, \
InputTextMessageContent, ChatAction, InlineKeyboardMarkup, \
InlineKeyboardButton, InlineQueryResultPhoto
# Twitter API
auth = tweepy.OAuthHandler(
'consumer_token',
'consumer_secret')
auth.set_access_token(
'key',
'secret')
# bot logo address
logo_address = "http://domain/retwittgram.png"
api = tweepy.API(
auth,
wait_on_rate_limit=True,
wait_on_rate_limit_notify=True,
retry_count=2,
retry_delay=2,
compression=True
)
# Telegram API
# access bot via token
updater = Updater(token='Telegram HTTP API Token')
dispatcher = updater.dispatcher
# extend and get tweet
def extend(link):
global tweet, extend_status
id = link.split('/')[-1].split('?')[0]
try:
tweet = api.get_status(id, tweet_mode='extended')
extend_status = True
except Exception:
print('Error on extend')
extend_status = False
return tweet, extend_status
def start(bot, update):
update.message.reply_text('Hi , send me tweet link')
return directresponse
def button(bot, update):
extend(update.callback_query.data)
callback_res = '❤️ : ' + str(tweet.favorite_count) + ' 🔁 : ' + \
str(tweet.retweet_count)
bot.answerCallbackQuery(
callback_query_id=update.callback_query.id,
text=callback_res)
# direct chat
def directresponse(bot, update):
try:
link = re.search(
'(?P<url>https?://[^\s]+)',
update.message.text).group('url').split('?')[0]
except Exception:
bot.send_message(
chat_id=update.message.chat_id,
reply_to_message_id=update.message.message_id,
text='❌'
)
bot.send_chat_action(
chat_id=update.message.chat_id,
action=ChatAction.TYPING)
extend(link)
if extend_status:
pure_text = re.sub(r"http\S+", '', tweet.full_text)
kb = [
[InlineKeyboardButton(text='🔗', url=str(link)),
InlineKeyboardButton(text='ℹ️', callback_data=str(link)),
InlineKeyboardButton(text='🔀', switch_inline_query=str(link))]
]
if tweet.is_quote_status:
response = pure_text + '\n\n' + ' 💬' + ' ' + \
tweet.quoted_status['user']['name'] + ' : ' + \
tweet.quoted_status['full_text'] + \
' \n\n 👤 <a href=\"' + link + '\">' + tweet.user.name + '</a>'
else:
response = tweet.full_text + ' \n\n 👤 <a href=\"' + \
link + '\">' + tweet.user.name + '</a>'
if 'media' in tweet.entities:
if (
tweet.entities['media'][0]['type'] == 'photo' and
not tweet.extended_entities['media'][0]['type'] == 'video'
):
response = pure_text + ' \n\n 👤 ' + tweet.user.name + \
'\n ' + tweet.entities['media'][0]['url']
if len(response) > 200:
response = '<a href=\"' + \
tweet.entities['media'][0]['media_url_https'] + \
'\">‍</a> ' + pure_text + ' \n\n 👤 ' + \
tweet.user.name + '\n ' +\
tweet.entities['media'][0]['url']
bot.send_message(
parse_mode='HTML',
chat_id=update.message.chat_id,
reply_markup=InlineKeyboardMarkup(
kb,
resize_keyboard=True),
reply_to_message_id=update.message.message_id,
text=response
)
else:
bot.sendPhoto(
photo=tweet.entities['media'][0]['media_url'],
chat_id=update.message.chat_id,
reply_markup=InlineKeyboardMarkup(
kb,
resize_keyboard=True),
reply_to_message_id=update.message.message_id,
caption=response
)
if tweet.extended_entities['media'][0]['video_info']['variants'][0]['content_type'] == 'video/mp4':
response = pure_text + ' \n\n 👤 ' + tweet.user.name + \
'\n ' + tweet.entities['media'][0]['url']
if len(response) > 200:
response = '<a href=\"' + \
tweet.extended_entities['media'][0]['video_info']['variants'][0]['url'] + \
'\">‍</a> ' + pure_text + ' \n\n 👤 ' + \
tweet.user.name + '\n ' +\
tweet.entities['media'][0]['url']
bot.send_message(
parse_mode='HTML',
chat_id=update.message.chat_id,
reply_markup=InlineKeyboardMarkup(
kb,
resize_keyboard=True),
reply_to_message_id=update.message.message_id,
text=response
)
else:
bot.send_video(
video=tweet.extended_entities['media'][0]['video_info']['variants'][0]['url'],
chat_id=update.message.chat_id,
reply_markup=InlineKeyboardMarkup(
kb,
resize_keyboard=True),
reply_to_message_id=update.message.message_id,
caption=response
)
elif tweet.extended_entities['media'][0]['video_info']['variants'][1]['content_type'] == 'video/mp4':
response = pure_text + ' \n\n 👤 ' + tweet.user.name + \
'\n ' + tweet.entities['media'][0]['url']
if len(response) > 200:
response = '<a href=\"' + \
tweet.extended_entities['media'][0]['video_info']['variants'][1]['url'] + \
'\">‍</a> ' + pure_text + ' \n\n 👤 ' + \
tweet.user.name + '\n ' +\
tweet.entities['media'][0]['url']
bot.send_message(
parse_mode='HTML',
chat_id=update.message.chat_id,
reply_markup=InlineKeyboardMarkup(
kb,
resize_keyboard=True),
reply_to_message_id=update.message.message_id,
text=response
)
else:
bot.send_video(
video=tweet.extended_entities['media'][0]['video_info']['variants'][1]['url'],
chat_id=update.message.chat_id,
reply_markup=InlineKeyboardMarkup(
kb,
resize_keyboard=True),
reply_to_message_id=update.message.message_id,
caption=response
)
else:
bot.send_message(
parse_mode='HTML',
disable_web_page_preview=True,
chat_id=update.message.chat_id,
reply_markup=InlineKeyboardMarkup(kb, resize_keyboard=True),
reply_to_message_id=update.message.message_id,
text=response
)
if not extend_status:
bot.send_message(
chat_id=update.message.chat_id,
reply_to_message_id=update.message.message_id,
text='❌'
)
# inline respond function
def inlinequery(bot, update):
results = list()
results.clear()
link = ''
# inline mode for zero character
if len(update.inline_query.query) <= 5:
results.clear()
results.append(InlineQueryResultArticle(
id=uuid4(),
title=" ▶️ Retwittgram",
description="type tweet link",
url="https://twitter.com/user/status/9876543210",
thumb_url=logo_address,
input_message_content=InputTextMessageContent(
" ♻️ @retwitgrambot sends your tweet beautifully ")))
# inline mode in normal case
else:
link = re.search(
'(?P<url>https?://[^\s]+)',
update.inline_query.query).group('url').split('?')[0]
extend(link)
pure_text = re.sub(r"http\S+", '', tweet.full_text)
if extend_status:
kb = [
[InlineKeyboardButton(text='🔗', url=link),
InlineKeyboardButton(text='ℹ️', callback_data=link),
InlineKeyboardButton(text='🔀', switch_inline_query=link)]
]
if tweet.is_quote_status:
response = pure_text + '\n\n' + ' 💬' + ' ' + \
tweet.quoted_status['user']['name'] + ' : ' + \
tweet.quoted_status['full_text'] + ' \n\n 👤 <a href=\"' + \
link + '\">' + tweet.user.name + '</a>'
else:
response = tweet.full_text + ' \n\n 👤 <a href=\"' + link + \
'\">' + tweet.user.name + '</a>'
results.append(InlineQueryResultArticle(
id=uuid4(),
title=tweet.user.name,
description=pure_text,
reply_markup=InlineKeyboardMarkup(
kb,
resize_keyboard=True),
input_message_content=InputTextMessageContent(
response,
parse_mode=ParseMode.HTML)))
update.inline_query.answer(results)
if 'media' in tweet.entities:
if (
tweet.entities['media'][0]['type'] == 'photo' and
not tweet.extended_entities['media'][0]['type'] == 'video'
):
response = '<a href=\"' + \
tweet.entities['media'][0]['media_url_https'] + \
'\">‍</a> ' + pure_text + ' \n\n 👤 ' + \
tweet.user.name + '\n ' + \
tweet.entities['media'][0]['url']
results.append(InlineQueryResultArticle(
id=uuid4(),
title=tweet.user.name,
description=tweet.full_text,
url=link,
thumb_url=tweet.entities['media'][0]['url'],
reply_markup=InlineKeyboardMarkup(
kb,
resize_keyboard=True),
input_message_content=InputTextMessageContent(
response,
parse_mode=ParseMode.HTML)))
update.inline_query.answer(results)
if tweet.extended_entities['media'][0]['video_info']['variants'][0]['content_type'] == 'video/mp4':
response = '<a href=\"' + \
tweet.extended_entities['media'][0]['video_info']['variants'][0]['url'] + \
'\">‍</a> ' + pure_text + ' \n\n 👤 ' + \
tweet.user.name + '\n ' +\
tweet.entities['media'][0]['url']
results.append(InlineQueryResultArticle(
id=uuid4(),
title=tweet.user.name,
description=tweet.full_text,
url=link,
thumb_url=tweet.entities['media'][0]['url'],
reply_markup=InlineKeyboardMarkup(
kb,
resize_keyboard=True),
input_message_content=InputTextMessageContent(
response,
parse_mode=ParseMode.HTML)))
update.inline_query.answer(results)
elif tweet.extended_entities['media'][0]['video_info']['variants'][1]['content_type'] == 'video/mp4':
response = '<a href=\"' + \
tweet.extended_entities['media'][0]['video_info']['variants'][1]['url'] + \
'\">‍</a> ' + pure_text + ' \n\n 👤 ' + \
tweet.user.name + '\n ' +\
tweet.entities['media'][0]['url']
results.append(InlineQueryResultArticle(
id=uuid4(),
title=tweet.user.name,
description=tweet.full_text,
url=link,
thumb_url=tweet.entities['media'][0]['url'],
reply_markup=InlineKeyboardMarkup(
kb,
resize_keyboard=True),
input_message_content=InputTextMessageContent(
response,
parse_mode=ParseMode.HTML)))
update.inline_query.answer(results)
if not extend_status:
results.append(InlineQueryResultArticle(
id=uuid4(),
title=" ▶️ Retwittgram",
description="❌",
url="https://twitter.com/user/status/9876543210",
thumb_url=logo_address,
input_message_content=InputTextMessageContent(
" ♻️ @retwitgrambot sends your tweet beautifully ")))
update.inline_query.answer(results)
# update inline respond
# cancel function
def cancel(bot, update):
bot.sendMessage(chat_id=update.message.chat_id, text="canceled")
# About function
def about(bot, update):
abouttext = " ♻️ @retwitgrambot sends your tweet beautifully \n" + \
"🔗 https://github.com/mostafaasadi/retwitgram"
bot.sendMessage(chat_id=update.message.chat_id, text=abouttext)
# help function
def helpf(bot, update):
bot.sendChatAction(
chat_id=update.message.chat_id,
action=ChatAction.RECORD_VIDEO)
bot.sendChatAction(
chat_id=update.message.chat_id,
action=ChatAction.UPLOAD_VIDEO)
bot.sendDocument(
chat_id=update.message.chat_id,
document='CgADBAADzgEAAhvd8FLkIjsOlU6tlwI')
def main():
# handlers
start_handler = CommandHandler('start', start)
second_handler = MessageHandler(Filters.text, directresponse)
cancel_handler = CommandHandler('cancel', cancel)
about_handler = CommandHandler('about', about)
help_handler = CommandHandler('help', helpf)
# handle dispatcher
dispatcher.add_handler(InlineQueryHandler(inlinequery))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
dispatcher.add_handler(start_handler)
dispatcher.add_handler(second_handler)
dispatcher.add_handler(about_handler)
dispatcher.add_handler(help_handler)
dispatcher.add_handler(cancel_handler)
# run
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()