-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
executable file
·100 lines (81 loc) · 3.14 KB
/
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
96
97
98
99
100
#!/usr/bin/env python
# Copyright (c) 2019 Steepo
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# -*- coding: utf-8 -*-
import logging
import os
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from datetime import datetime
from pathlib import Path
# slugify
import unicodedata
import re
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logger = logging.getLogger("RedBullBot")
def start(bot, update):
"""Send a message when the command /start is issued."""
logger.info(f"Start issued by: {get_user_name(update.message.from_user)}")
update.message.reply_text('Ciao, mandami una foto mentre bevi una Red Bull', quote=True)
def help(bot, update):
"""Send a message when the command /help is issued."""
logger.info(f"Help issued by: {get_user_name(update.message.from_user)}")
update.message.reply_text('Mandami una foto mentre bevi una Red Bull', quote=True)
def slugify(value, allow_unicode=False):
"""
https://github.com/django/django/blob/master/django/utils/text.py
Convert to ASCII if 'allow_unicode' is False. Convert spaces to hyphens.
Remove characters that aren't alphanumerics, underscores, or hyphens.
Convert to lowercase. Also strip leading and trailing whitespace.
"""
value = str(value)
if allow_unicode:
value = unicodedata.normalize('NFKC', value)
else:
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s-]', '', value).strip().lower()
return re.sub(r'[-\s]+', '-', value)
def get_user_name(user):
name = None
if user.username:
name = user.username
else:
name = user.first_name
if user.last_name:
name = f"{name} {user.last_name}"
return name
def save_photo(bot, update):
logger.info(f"Photo sent by: {get_user_name(update.message.from_user)}")
file_id = update.message.photo[-1].file_id
photo = bot.get_file(file_id)
update.message.reply_text("Grazie per la foto, goditi la tua Red Bull!", quote=True)
timestamp = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
valid_user_name = slugify(get_user_name(update.message.from_user))
filename = f"{timestamp}_{valid_user_name}_{update.message.message_id}.png"
photo.download(Path(f"./pic/{filename}"))
def error(bot, update, error):
"""Log Errors caused by Updates."""
logger.error(f'Update "{update}" caused error "{error}"')
def main():
"""Start the bot."""
logger.info("The bot is starting...")
BOT_TOKEN = os.environ.get("BOT_TOKEN")
if BOT_TOKEN is None:
logger.critical("Please set the BOT_TOKEN env variable")
return
updater = Updater(BOT_TOKEN)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
# photo handler
dp.add_handler(MessageHandler(Filters.photo, save_photo))
dp.add_error_handler(error)
logger.info("The bot is listening...")
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()