-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.py
282 lines (225 loc) · 10.4 KB
/
handler.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
'''
Main handler for all the incoming bot events
'''
import logging
from concurrent.futures import ThreadPoolExecutor
import telegram
from telegram.ext import Updater, MessageHandler, CommandHandler, Filters, CallbackQueryHandler
from telegram.ext.inlinequeryhandler import InlineQueryHandler
from telegram.ext.choseninlineresulthandler import ChosenInlineResultHandler
import boto3
from boto3.dynamodb.conditions import Key, Attr
import config
from consts import RESPONSES, COMMANDS
from db_actions import (update_users_followers, follow_user, unfollow_user,
create_user, update_user_photo, update_user, get_followers_list)
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Create a connection to the database
table = None
def message_handler(bot, update):
'''
Handler for the text messages
'''
logger.info(update)
logger.info('message_handler')
pass
def contact_handler(bot, update):
'''
Handler for the messages with contacts
'''
username = str(update['message']['chat']['id'])
user_to_follow = str(update['message']['contact']['user_id'])
if not update['message']['contact']['user_id']:
bot.send_message(username, RESPONSES['empty_contact'])
return
new_follower = follow_user(username, user_to_follow, table)
if new_follower:
bot.send_message(user_to_follow, RESPONSES['new_follower'])
logger.info('contat_handler')
def start_command_handler(bot, update):
'''
Handler for the "start" command.
Add current user to the Users table
'''
# Avoid duplication of the existing users
username = str(update['message']['chat']['id'])
create_user(update, table)
photo = bot.getUserProfilePhotos(update.message.from_user.id)['photos'][0]
update_user_photo(photo, username, table)
logger.info('start_command_handler')
def update_command_handler(bot, update):
'''
Handler for the "update" commands
Update user info in database
'''
username = str(update['message']['chat']['id'])
update_user(update, table)
photo = bot.getUserProfilePhotos(update.message.from_user.id)['photos'][0]
update_user_photo(photo, username, table)
logger.info('update_command_handler')
def send_all_command_handler(bot, update):
username = str(update['message']['chat']['id'])
if not username == config.MAIN_USER:
return
all_users = table.scan()['Items']
message = update['message']['text'][len('/send_all'):]
with ThreadPoolExecutor(max_workers=min(len(all_users), config.MAX_THREADS)) as Executor:
list(Executor.map(lambda x: bot.send_message(*x),
[(int(user['username']), RESPONSES['important_message'].format(message))
for user in all_users]))
def remove_command_handler(bot, update):
'''
Handler for the "remove" commands
Remove user(s) from the current user following list
'''
chat_id = update['message']['chat']['id']
username = str(update['message']['chat']['id'])
users = get_followers_list(username, table)
if not users:
bot.send_message(chat_id, RESPONSES['empty_remove_command'])
return
logger.info(users)
buttons = [telegram.InlineKeyboardButton(text='%s %s' % (user.get('first_name', ''), user.get('last_name', '')),
callback_data=str(user['username'])) for user in users]
reply_markup = telegram.InlineKeyboardMarkup([[button] for button in buttons])
bot.sendMessage(chat_id=chat_id,
text=RESPONSES['remove_keyboard_message'],
reply_markup=reply_markup)
logger.info('remove_command_handler')
def remove_user_callback(bot, update):
'''
Handler callback from custom keyboard for the "remove" commands
Remove user from the current user following list
'''
logger.info('='*80)
username = str(update['callback_query']['message']['chat']['id'])
unfollower_id = str(update['callback_query']['data'])
logger.info("remove users %s %s" % (username, unfollower_id))
# update_user_real_follow_count(username, follow=new_follow)
unfollow_user(username, unfollower_id, table)
def send_command_handler(bot, update):
'''
Handler for the "send" command
Send message to all the followers who has more that 10 real_following
'''
message = update['message']['text'][len('/send'):]
if not message:
chat_id = update['message']['chat']['id']
bot.send_message(chat_id, RESPONSES['empty_send_command'])
return
username = str(update['message']['chat']['id'])
users_to_send = table.scan(FilterExpression=Attr('follow').contains(username))['Items']
if not users_to_send:
return
with ThreadPoolExecutor(max_workers=min(len(users_to_send), config.MAX_THREADS)) as Executor:
list(Executor.map(lambda x: bot.send_message(*x),
[(int(user['username']), RESPONSES['message_boilerplate'].format(message))
for user in users_to_send]))
# for user in users_to_send:
# bot.send_message(int(user['username']), f'Somebody told me, that "{message}"')
logger.info('send_command_handler')
def photo_handler(bot, update):
'''
Handler for the photo messages
Send message with photo to all the followers who has more that 10 real_following
'''
photo = update['message']['photo']
username = str(update['message']['chat']['id'])
users_to_send = table.scan(FilterExpression=Attr('follow').contains(username))['Items']
if not users_to_send:
return
photo_to_send = photo[-1]['file_id']
with ThreadPoolExecutor(max_workers=min(len(users_to_send), config.MAX_THREADS)) as Executor:
list(Executor.map(lambda x: bot.send_photo(*x),
[(int(user['username']), photo_to_send, RESPONSES['photo_caption'])
for user in users_to_send]))
logger.info('send_photo_handler')
def document_handler(bot, update):
'''
Handler for the document messages
Send message with photo to all the followers who has more that 10 real_following
'''
document = update['message']['document']['file_id']
username = str(update['message']['chat']['id'])
users_to_send = table.scan(FilterExpression=Attr('follow').contains(username))['Items']
if not users_to_send:
return
with ThreadPoolExecutor(max_workers=min(len(users_to_send), config.MAX_THREADS)) as Executor:
list(Executor.map(lambda x: bot.send_document(*x),
[(int(user['username']), document)
for user in users_to_send]))
logger.info('send_document_handler')
def sticker_handler(bot, update):
'''
Handler for the sticker messages
Send message with sticker to all the followers who has more that 10 real_following
'''
def send_message_and_sticker(chat_id):
'''
Just a little handler to be sure that sticker will be send
after the message
'''
bot.send_message(chat_id, RESPONSES['before_sticker_send'])
bot.send_sticker(chat_id, sticker)
username = str(update['message']['chat']['id'])
sticker = update['message']['sticker']['file_id']
users_to_send = table.scan(FilterExpression=Attr('follow').contains(username))['Items']
if not users_to_send:
return
with ThreadPoolExecutor(max_workers=min(len(users_to_send), config.MAX_THREADS)) as Executor:
list(Executor.map(send_message_and_sticker,
[int(user['username']) for user in users_to_send]))
logger.info('send_sticker_handler')
def inline_query_handler(bot, update):
query = update.inline_query.query
inline_query_id = update.inline_query.id
if len(query) < 3:
bot.answerInlineQuery(inline_query_id, [])
return
query_result = table.scan(FilterExpression=Attr('first_name').contains(query.upper()) |
Attr('last_name').contains(query.upper()))['Items']
query_articles = list(map(lambda x: telegram.InlineQueryResultArticle(x['username'],
'%s %s' % (x['first_name'] or '', x['last_name'] or ''),
telegram.InputTextMessageContent('%s %s' % (x['first_name'], x['last_name']))),
query_result))
bot.answerInlineQuery(inline_query_id, query_articles)
def inline_query_result_handler(bot, update):
username = str(update.chosen_inline_result.from_user.id)
user_to_follow = str(update.chosen_inline_result.result_id)
new_follower = follow_user(username, user_to_follow, table)
if new_follower:
bot.send_message(user_to_follow, RESPONSES['new_follower'])
logger.info('Query result handler')
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb', region_name=config.DB_REGION)
globals()['table'] = dynamodb.Table(config.DB_NAME)
logger.info(f'HANDLER UPDATE {event} {context}')
updater = Updater(config.BOT_TOKEN)
bot = telegram.Bot(config.BOT_TOKEN)
dp = updater.dispatcher
register_handlers(dp)
dp.process_update(telegram.Update.de_json(event, bot))
def main():
dynamodb = boto3.resource('dynamodb', endpoint_url=config.DB_HOST)
globals()['table'] = dynamodb.Table(config.DB_NAME)
updater = Updater(config.TEST_BOT_TOKEN)
# Get the dispatcher to register handlers
dp = updater.dispatcher
register_handlers(dp)
updater.start_polling()
updater.idle()
def register_handlers(dp):
for command in COMMANDS:
dp.add_handler(CommandHandler(command, globals()[f'{command}_command_handler']))
dp.add_handler(MessageHandler(Filters.contact, contact_handler))
dp.add_handler(MessageHandler(Filters.photo, photo_handler))
dp.add_handler(MessageHandler(Filters.document, document_handler))
dp.add_handler(MessageHandler(Filters.sticker, sticker_handler))
dp.add_handler(InlineQueryHandler(inline_query_handler))
dp.add_handler(ChosenInlineResultHandler(inline_query_result_handler))
dp.add_handler(CallbackQueryHandler(remove_user_callback))
if __name__ == '__main__':
main()