forked from franciscod/telegram-twitter-forwarder-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
306 lines (241 loc) · 10.1 KB
/
commands.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
import json
from datetime import datetime
from pytz import timezone
from pytz.exceptions import UnknownTimeZoneError
import telegram
from telegram.emoji import Emoji
import tweepy
from tweepy.auth import OAuthHandler
from tweepy.error import TweepError
from models import Subscription
from util import with_touched_chat, escape_markdown, markdown_twitter_usernames
TIMEZONE_LIST_URL = "https://en.wikipedia.org/wiki/List_of_tz_database_time_zones"
def cmd_ping(bot, update):
bot.reply(update, 'Pong!')
@with_touched_chat
def cmd_start(bot, update, chat=None):
bot.reply(
update,
"Hello! This bot lets you subscribe to twitter accounts and receive their tweets here! "
"Check out /help for more info.")
@with_touched_chat
def cmd_help(bot, update, chat=None):
bot.reply(update, """
Hello! This bot forwards you updates from twitter streams!
Here's the commands:
- /sub - subscribes to updates from users
- /unsub - unsubscribes from users
- /list - lists current subscriptions
- /export - sends you a /sub command that contains all current subscriptions
- /all - shows you the latest tweets from all subscriptions
- /wipe - remove all the data about you and your subscriptions
- /auth - start Twitter authorization process
- /verify - send Twitter verifier code to complete authorization process
- /export\_friends - generate /sub command to subscribe to all your Twitter friends (authorization required)
- /set\_timezone - set your [timezone name]({}) (for example Asia/Tokyo)
- /source - info about source code
- /help - view help text
This bot is free open source software, check /source if you want to host it!
""".format(
TIMEZONE_LIST_URL),
disable_web_page_preview=True,
parse_mode=telegram.ParseMode.MARKDOWN)
@with_touched_chat
def cmd_sub(bot, update, args, chat=None):
if len(args) < 1:
bot.reply(update, "Use /sub username1 username2 username3 ...")
return
tw_usernames = args
not_found = []
already_subscribed = []
successfully_subscribed = []
for tw_username in tw_usernames:
tw_user = bot.get_tw_user(tw_username)
if tw_user is None:
not_found.append(tw_username)
continue
if Subscription.select().where(
Subscription.tw_user == tw_user,
Subscription.tg_chat == chat).count() == 1:
already_subscribed.append(tw_user.full_name)
continue
Subscription.create(tg_chat=chat, tw_user=tw_user)
successfully_subscribed.append(tw_user.full_name)
reply = ""
if len(not_found) is not 0:
reply += "Sorry, I didn't find username{} {}\n\n".format(
"" if len(not_found) is 1 else "s",
", ".join(not_found)
)
if len(already_subscribed) is not 0:
reply += "You're already subscribed to {}\n\n".format(
", ".join(already_subscribed)
)
if len(successfully_subscribed) is not 0:
reply += "I've added your subscription to {}".format(
", ".join(successfully_subscribed)
)
bot.reply(update, reply)
@with_touched_chat
def cmd_unsub(bot, update, args, chat=None):
if len(args) < 1:
bot.reply(update, "Use /unsub username1 username2 username3 ...")
return
tw_usernames = args
not_found = []
successfully_unsubscribed = []
for tw_username in tw_usernames:
tw_user = bot.get_tw_user(tw_username)
if tw_user is None or Subscription.select().where(
Subscription.tw_user == tw_user,
Subscription.tg_chat == chat).count() == 0:
not_found.append(tw_username)
continue
Subscription.delete().where(
Subscription.tw_user == tw_user,
Subscription.tg_chat == chat).execute()
successfully_unsubscribed.append(tw_user.full_name)
reply = ""
if len(not_found) is not 0:
reply += "I didn't find any subscription to {}\n\n".format(
", ".join(not_found)
)
if len(successfully_unsubscribed) is not 0:
reply += "You are no longer subscribed to {}".format(
", ".join(successfully_unsubscribed)
)
bot.reply(update, reply)
@with_touched_chat
def cmd_list(bot, update, chat=None):
subscriptions = list(Subscription.select().where(
Subscription.tg_chat == chat))
if len(subscriptions) == 0:
return bot.reply(update, 'You have no subscriptions yet! Add one with /sub username')
subs = ['']
for sub in subscriptions:
subs.append(sub.tw_user.full_name)
subject = "This group is" if chat.is_group else "You are"
bot.reply(
update,
subject + " subscribed to the following Twitter users:\n" +
"\n - ".join(subs) + "\n\nYou can remove any of them using /unsub username")
@with_touched_chat
def cmd_export(bot, update, chat=None):
subscriptions = list(Subscription.select().where(
Subscription.tg_chat == chat))
if len(subscriptions) == 0:
return bot.reply(update, 'You have no subscriptions yet! Add one with /sub username')
subs = ['']
for sub in subscriptions:
subs.append(sub.tw_user.screen_name)
subject = "Use this to subscribe to all subscribed Twitter users in another chat:\n\n"
bot.reply(
update,
subject + "/sub " + " ".join(subs))
@with_touched_chat
def cmd_wipe(bot, update, chat=None):
subscriptions = list(Subscription.select().where(
Subscription.tg_chat == chat))
subs = "You had no subscriptions."
if subscriptions:
subs = ''.join([
"For the record, you were subscribed to these users: ",
', '.join((s.tw_user.screen_name for s in subscriptions)),
'.'])
bot.reply(update, "Okay, I'm forgetting about this chat. " + subs +
" Come back to me anytime you want. Goodbye!")
chat.delete_instance(recursive=True)
@with_touched_chat
def cmd_source(bot, update, chat=None):
bot.reply(update, "This bot is Free Software under the LGPLv3. "
"You can get the code from here: "
"https://github.com/franciscod/telegram-twitter-forwarder-bot")
@with_touched_chat
def cmd_all(bot, update, chat=None):
subscriptions = list(Subscription.select().where(
Subscription.tg_chat == chat))
if len(subscriptions) == 0:
return bot.reply(update, 'You have no subscriptions, so no tweets to show!')
text = ""
for sub in subscriptions:
if sub.last_tweet is None:
text += "\n{screen_name}: <no tweets yet>".format(
screen_name=escape_markdown(sub.tw_user.screen_name),
)
else:
text += ("\n{screen_name}:\n{text} "
"[link](https://twitter.com/{screen_name}/status/{tw_id})").format(
text=markdown_twitter_usernames(escape_markdown(sub.last_tweet.text)),
tw_id=sub.last_tweet.tw_id,
screen_name=escape_markdown(sub.tw_user.screen_name),
)
bot.reply(update, text,
disable_web_page_preview=True,
parse_mode=telegram.ParseMode.MARKDOWN)
@with_touched_chat
def cmd_get_auth_url(bot, update, chat):
auth = OAuthHandler(bot.tw.auth.consumer_key, bot.tw.auth.consumer_secret)
auth_url = auth.get_authorization_url()
chat.twitter_request_token = json.dumps(auth.request_token)
chat.save()
msg = "go to [this url]({}) and send me your verifier code using /verify code"
bot.reply(update, msg.format(auth_url),
parse_mode=telegram.ParseMode.MARKDOWN)
@with_touched_chat
def cmd_verify(bot, update, args, chat):
if not chat.twitter_request_token:
bot.reply(update, "Use /auth command first")
return
if len(args) < 1:
bot.reply(update, "No verifier code specified")
return
verifier_code = args[0]
auth = OAuthHandler(bot.tw.auth.consumer_key, bot.tw.auth.consumer_secret)
auth.request_token = json.loads(chat.twitter_request_token)
try:
auth.get_access_token(verifier_code)
except TweepError:
bot.reply(update, "Invalid verifier code. Use /auth again")
return
chat.twitter_token = auth.access_token
chat.twitter_secret = auth.access_token_secret
chat.save()
bot.reply(update, "Access token setup complete")
api = tweepy.API(auth)
settings = api.get_settings()
tz_name = settings.get("time_zone", {}).get("tzinfo_name")
cmd_set_timezone(bot, update, [tz_name])
@with_touched_chat
def cmd_export_friends(bot, update, chat):
if not chat.is_authorized:
if not chat.twitter_request_token:
bot.reply(update, "You have not authorized yet. Use /auth to do it")
else:
bot.reply(update, "You have not verified your authorization yet. Use /verify code to do it")
return
bot_auth = bot.tw.auth
api = chat.tw_api(bot_auth.consumer_key, bot_auth.consumer_secret)
screen_names = [f.screen_name for f in tweepy.Cursor(api.friends).items()]
bot.reply(update, "Use this to subscribe to all your Twitter friends:")
bot.reply(update, "/sub {}".format(" ".join(screen_names)))
@with_touched_chat
def cmd_set_timezone(bot, update, args, chat):
if len(args) < 1:
bot.reply(update,
"No timezone specified. Find yours [here]({})!".format(TIMEZONE_LIST_URL),
parse_mode=telegram.ParseMode.MARKDOWN)
return
tz_name = args[0]
try:
tz = timezone(tz_name)
chat.timezone_name = tz_name
chat.save()
tz_str = datetime.now(tz).strftime('%Z %z')
bot.reply(update, "Timezone is set to {}".format(tz_str))
except UnknownTimeZoneError:
bot.reply(update,
"Unknown timezone. Find yours [here]({})!".format(TIMEZONE_LIST_URL),
parse_mode=telegram.ParseMode.MARKDOWN)
@with_touched_chat
def handle_chat(bot, update, chat=None):
bot.reply(update, "Hey! Use commands to talk with me, please! See /help")