-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
448 lines (368 loc) · 14.4 KB
/
main.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=logging-fstring-interpolation
# pylint: disable=broad-exception-caught
# pylint: disable=missing-class-docstring
"""
Aligner Telegram Bot
"""
import datetime
import logging
import os
import pytz
from pyairtable import Api
from telegram import Bot, InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import (
Application,
CallbackContext,
CallbackQueryHandler,
CommandHandler,
ContextTypes,
ConversationHandler,
MessageHandler,
filters,
)
SAVE_KUDO = 0
TG_BOT_TOK = os.environ["TG_BOT_TOK"]
TG_GROUP_ID = -int(os.environ["TG_GROUP_ID"])
TG_THREAD_ID = int(os.environ["TG_THREAD_ID"])
TG_THREAD2_ID = int(os.environ["TG_THREAD2_ID"])
AIRTABLE_TOK = os.environ["AIRTABLE_TOK"]
AIRTABLE_BASE_ID = os.environ["AIRTABLE_BASE_ID"]
AIRTABLE_DB_PART_ID = os.environ["AIRTABLE_DB_PART_ID"]
AIRTABLE_DB_VOTE_ID = os.environ["AIRTABLE_DB_VOTE_ID"]
airtable_api = Api(AIRTABLE_TOK)
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
def get_today() -> str:
mountain_time = pytz.timezone("America/Denver")
now_in_mountain_time = datetime.datetime.now(mountain_time)
return now_in_mountain_time.strftime("%Y-%m-%d")
async def greet_new_users(update: Update, context: CallbackContext):
logger.info("greeting")
bot_username = context.bot.username
message = f"""
🤗 Welcome to our newcomers\\!
To register to the game and be eligible for rewards, \
[send me a DM](https://t.me/{bot_username}?start)\\.
Please read the pinned message to learn more\\.
"""
await context.bot.send_message(
chat_id=TG_GROUP_ID,
message_thread_id=TG_THREAD_ID,
text=message,
parse_mode="MarkdownV2",
disable_web_page_preview=True,
)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
logger.info(f"start {update.effective_user.name}")
user = update.effective_user
bot: Bot = context.bot
if update.effective_chat.type != "private" and update.message:
await update.message.reply_markdown_v2(
f"👋 Hi {user.name}\\! "
f"Let's continue the conversation [in DMs]"
f"(https://t.me/{context.bot.username})\\."
)
await bot.send_message(
chat_id=user.id,
text=f"Hi {user.name}, ready to join the game? Just hit /join.",
reply_markup=InlineKeyboardMarkup(
[[InlineKeyboardButton("Join", callback_data="/join")]]
),
)
async def join(update: Update, context: ContextTypes.DEFAULT_TYPE):
logger.info(f"join {update.effective_user.name}")
user = update.effective_user
part_table = airtable_api.table(AIRTABLE_BASE_ID, AIRTABLE_DB_PART_ID)
fields = {
"Telegram ID": user.id,
"Telegram handle": user.username,
"Telegram name": user.full_name,
}
try:
existing = part_table.all(formula=f"{{Telegram ID}}={fields['Telegram ID']}")
if not existing:
part_table.create(fields)
except Exception:
logger.exception(f"join-existing {update.effective_user.name}")
await context.bot.send_message(
chat_id=user.id, text="🤦1️⃣ An unknown error occurred, we're on it."
)
return
if existing:
await context.bot.send_message(
chat_id=user.id,
text=(
"✅ You're already in!\n"
"Hit /kudo "
"when you're ready to start sending kudos."
),
)
return
async def start_kudo(update: Update, context: ContextTypes.DEFAULT_TYPE):
logger.info(f"kudo {update.effective_user.name}")
user = update.effective_user
part_table = airtable_api.table(AIRTABLE_BASE_ID, AIRTABLE_DB_PART_ID)
kudo_table = airtable_api.table(AIRTABLE_BASE_ID, AIRTABLE_DB_VOTE_ID)
try:
existing_part = part_table.all(formula=f"{{Telegram ID}}={user.id}")
except Exception:
logger.exception(f"start_kudo-existing_part {update.effective_user.name}")
await context.bot.send_message(
chat_id=user.id, text="🤦2️⃣ An unknown error occurred, we're on it."
)
return ConversationHandler.END
if not existing_part:
await context.bot.send_message(
chat_id=user.id,
text=(
"🤌 You need to join first. Join first by hitting Join "
"or sending the /join command."
),
reply_markup=InlineKeyboardMarkup(
[[InlineKeyboardButton("Join", callback_data="/join")]]
),
)
return ConversationHandler.END
part_id = existing_part[0]["id"]
today = get_today()
# part_ID = existing_part[0]["fields"]["ID"] # pylint: disable=invalid-name
# try:
# existing_kudos = kudo_table.all(formula=f"FIND({part_ID},{{Participant}})")
# existing_kudos_today = [
# k for k in existing_kudos if k["fields"]["Date"] == today
# ]
# except Exception:
# logger.exception(f"start_kudo-existing_kudos {update.effective_user.name}")
# await context.bot.send_message(
# chat_id=user.id, text="🤦3️⃣ An unknown error occurred, we're on it."
# )
# return ConversationHandler.END
#
# if len(existing_kudos_today) >= 3:
# names = [k["fields"]["Kudoee Telegram handle"] for k in existing_kudos_today]
# # [TODO] Add a reset procedure but this needs to be tied to a date.
# await context.bot.send_message(
# chat_id=user.id,
# text=(
# f"💗 You already gave your 3 kudos today, to {', '.join(names)}.\n"
# "🙏 Please get in touch with the team to reset your Kudos for today."
# ),
# )
# return ConversationHandler.END
if not context.args:
await context.bot.send_message(
chat_id=user.id,
text=(
"Please write the Telegram handle of the person you'd like to give a kudo to."
"\nIf you don't want to give kudos anymore, just send /cancel."
),
)
context.user_data["part_id"] = part_id
context.user_data["day"] = today
return SAVE_KUDO
kudoee_name = context.args[0]
kudo_fields = {
"Participant": [part_id],
"Kudoee Telegram handle": kudoee_name,
"Date": today,
}
try:
kudo_table.create(kudo_fields)
except Exception:
logger.exception(f"save_kudo-create {update.effective_user.name}")
await context.bot.send_message(
chat_id=user.id, text="🤦4️⃣ An unknown error occurred, we're on it."
)
return ConversationHandler.END
try:
await context.bot.send_message(
chat_id=user.id,
text=f"💌 Thank you for registering your appreciation to {kudoee_name}!",
)
except Exception:
logger.info(f"kudo_argument-dm_fail {update.effective_user.name}")
return ConversationHandler.END
async def unsafe_save_kudo(update: Update, context: ContextTypes.DEFAULT_TYPE):
logger.info(f"save_kudo {update.effective_user.name}")
name = update.message.text
user = update.effective_user
kudo_table = airtable_api.table(AIRTABLE_BASE_ID, AIRTABLE_DB_VOTE_ID)
part_id = context.user_data["part_id"]
del context.user_data["part_id"]
day = context.user_data["day"]
del context.user_data["day"]
fields = {
"Participant": [part_id],
"Kudoee Telegram handle": name,
"Date": day,
}
try:
kudo_table.create(fields)
except Exception:
logger.exception(f"save_kudo-create {update.effective_user.name}")
await context.bot.send_message(
chat_id=user.id, text="🤦4️⃣ An unknown error occurred, we're on it."
)
return ConversationHandler.END
await context.bot.send_message(
chat_id=user.id,
text=f"💌 Thank you for registering your appreciation to {name}!",
)
return ConversationHandler.END
async def cancel_kudo(update: Update, context: ContextTypes.DEFAULT_TYPE):
logger.info(f"cancel_kudo {update.effective_user.name}")
user = update.effective_user
await context.bot.send_message(chat_id=user.id, text="Kudo operation canceled.")
return ConversationHandler.END
async def send_zucash(update: Update, context: ContextTypes.DEFAULT_TYPE):
logger.info(f"send_zucash {update.effective_user.name} {context.args}")
if not context.args:
return
name = context.args[0]
user = update.effective_user
today = get_today()
part_table = airtable_api.table(AIRTABLE_BASE_ID, AIRTABLE_DB_PART_ID)
kudo_table = airtable_api.table(AIRTABLE_BASE_ID, AIRTABLE_DB_VOTE_ID)
part_fields = {
"Telegram ID": user.id,
"Telegram handle": user.username,
"Telegram name": user.full_name,
}
try:
existing_part = part_table.all(
formula=f"{{Telegram ID}}={part_fields['Telegram ID']}"
)
except Exception:
logger.exception(f"send_zucash-existing_part {update.effective_user.name}")
return
if not existing_part:
try:
part_table.create(part_fields)
except Exception:
logger.exception(f"send_zucash-create_part {update.effective_user.name}")
return
try:
existing_part = part_table.all(
formula=f"{{Telegram ID}}={part_fields['Telegram ID']}"
)
except Exception:
logger.exception(f"send_zucash-existing_part2 {update.effective_user.name}")
return
part_id = existing_part[0]["id"]
kudo_fields = {
"Participant": [part_id],
"Kudoee Telegram handle": name,
"Date": today,
"Zucash": True,
}
try:
kudo_table.create(kudo_fields)
except Exception:
logger.exception(f"send_zucash-create {update.effective_user.name}")
return
try:
await context.bot.send_message(
chat_id=user.id,
text=f"💌 Thank you for registering your appreciation to {name}!",
)
except Exception:
logger.info(f"send_zucash-no_dm {user.name}")
async def catch_all(update: Update, context: CallbackContext):
if not update.message or update.message.chat.type != "private":
return
logger.info(f"catchall {update.effective_user.name}")
user = update.effective_user
table = airtable_api.table(AIRTABLE_BASE_ID, AIRTABLE_DB_PART_ID)
try:
existing = table.all(formula=f"{{Telegram ID}}={user.id}")
except Exception as e: # pylint: disable=broad-exception-caught
logger.exception(e)
await context.bot.send_message(
chat_id=user.id, text="🤦5️⃣ An unknown error occurred, we're on it."
)
return
complement_text = (
"To send kudos, hit /kudo."
if existing
else "If you want to join the game, hit /join."
)
try:
await context.bot.send_message(
chat_id=user.id,
text=("🤷 Command not understood.\n" + complement_text),
)
except Exception: # pylint: disable=broad-exception-caught
logger.info(f"cancel_kudo-no_dm {update.effective_user.name}")
async def button_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
logger.info(f"join {update.effective_user.name}")
query = update.callback_query
if query.data == "/start":
await start(update, context)
if query.data == "/join":
await join(update, context)
if query.data == "/kudo":
await join(start_kudo, context)
async def log_all_updates(update: Update, context: ContextTypes.DEFAULT_TYPE):
user = update.effective_user
if user:
user_info = f"user {user.id} ({user.username})"
else:
user_info = "unknown user"
logging.info(f"Update from {user_info}: {update.to_dict()}") #
class DmOrGroupThreadFilter(filters.BaseFilter):
def __init__(self, group_id, thread_ids=None):
self.group_id = group_id
if thread_ids:
self.thread_ids = thread_ids
else:
self.thread_ids = []
super().__init__(name=None, data_filter=False)
def check_update(self, update: Update):
message = update.message
if message is None:
return False
is_dm = message.chat.type == "private"
is_target_group = message.chat_id == self.group_id
if self.thread_ids:
is_target_thread = message.message_thread_id in self.thread_ids
return is_dm or (is_target_group and is_target_thread)
return is_dm or is_target_group
def main() -> None:
# Create the Application and pass it your bot's token.
tg_app = Application.builder().token(TG_BOT_TOK).build()
msg_filter = DmOrGroupThreadFilter(
group_id=TG_GROUP_ID, thread_ids=[TG_THREAD_ID, TG_THREAD2_ID]
)
tg_app.add_handler(CommandHandler("greet", greet_new_users, filters=msg_filter))
tg_app.add_handler(CommandHandler("start", start, filters=msg_filter))
tg_app.add_handler(CommandHandler("join", join, filters=msg_filter))
tg_app.add_handler(
ConversationHandler(
entry_points=[CommandHandler("kudo", start_kudo, filters=msg_filter)],
states={
SAVE_KUDO: [
MessageHandler(
msg_filter & filters.TEXT & ~filters.COMMAND, unsafe_save_kudo
)
]
},
fallbacks=[
CommandHandler("cancel", cancel_kudo, filters=msg_filter),
MessageHandler(msg_filter & filters.COMMAND, unsafe_save_kudo),
],
)
)
tg_app.add_handler(CommandHandler("send_zucash", send_zucash, filters=msg_filter))
tg_app.add_handler(CallbackQueryHandler(button_callback))
tg_app.add_handler(MessageHandler(msg_filter, catch_all))
tg_app.add_handler(MessageHandler(msg_filter, log_all_updates), group=1)
# Run the bot until the user presses Ctrl-C
tg_app.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
main()