forked from mishka/TwitterMedia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwittermd_Bot.py
95 lines (74 loc) · 2.74 KB
/
twittermd_Bot.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
import re
from datetime import datetime
from TwitterMedia import TwitterMedia
from telegram import ParseMode
from telegram.ext import Updater, MessageHandler, CommandHandler
Pattern = r'(https?://)?(mobile.)?twitter.com/.+?/status/\d+'
Downloader = TwitterMedia()
def log(text):
print(f"| {datetime.now().strftime('%H:%M:%S')} | {text}")
def start(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = "Hi, send a twitter status url that contains a video/gif to use this bot.\nIf you face any issues please message @ulfsjar\n(please say hi im so lonely)"
)
def processor(update, context):
text = update.message.text
chat_id = update.message.chat_id
name = update.message.chat.first_name
username = update.message.chat.username
message_id = update.message.message_id
log(f'Request: {username} | {name} | {text}')
result = re.search(Pattern, text)
if not result:
context.bot.send_message(
chat_id = chat_id,
reply_to_message_id = message_id,
text = "_I'm sorry, I couldn't find a twitter url in your message._",
parse_mode = ParseMode.MARKDOWN
)
return
try:
tweet = Downloader.fetch_media(result.group(0))
except:
context.bot.send_message(
chat_id = chat_id,
reply_to_message_id = message_id,
text = "_I'm sorry, something went wrong while trying to find the video._",
parse_mode = ParseMode.MARKDOWN
)
return
if tweet is not None:
try:
context.bot.send_video(
chat_id = chat_id,
video = tweet.url,
reply_to_message_id = message_id,
supports_streaming = True
)
except:
context.bot.send_message(
chat_id = chat_id,
reply_to_message_id = message_id,
text = f"I'm sorry, telegram is being a lil meanie and not accepting the video.\n\nHere's the URL instead;\n\n{tweet.url}"
)
else:
context.bot.send_message(
chat_id = chat_id,
reply_to_message_id = message_id,
text = "_I'm sorry, something went wrong while trying to find the video._",
parse_mode = ParseMode.MARKDOWN
)
def main():
updater = Updater(
token = '',
use_context = True,
request_kwargs = {'read_timeout': 1000, 'connect_timeout': 1000}
)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(MessageHandler(None, processor))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()