-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
85 lines (58 loc) · 2.34 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
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from decouple import config
import logging
import os
from face_substitution import replace_faces
WELCOME_MESSAGE = "Welcome!\nSend an image with some faces to begin"
PORT = config("PORT", default=5000, cast=int)
TOKEN = config("TOKEN")
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
def start(update, context):
logger.info("Bot Started")
update.message.reply_text(WELCOME_MESSAGE)
def help(update, context):
update.message.reply_text("Send an image with some faces to begin")
def error(update, context):
logger.warning(f'Update "{update}" caused error "{context.error}"')
def catify(update, context):
def remove_image(filename):
if os.path.exists(filename):
os.remove(filename)
photos = update.message.photo
if photos:
user = update.message.from_user
update.message.reply_text("I'm working on it...")
logger.info(f"Photo received from {user.first_name} {user.last_name}")
# Download the best photo version
photo_id = photos[-1].file_id
context.bot.get_file(photo_id).download("image.png")
if replace_faces("image.png"):
# Face substitution was successful
update.message.reply_photo(open("image_with_cat.png", "rb"))
remove_image("image_with_cat.png")
else:
# Face substitution was unsuccessful
update.message.reply_text("I didn't find any face!")
remove_image("image.png")
def main():
# Initialize the bot
updater = Updater(token=TOKEN, use_context=True)
# Get the update dispatcher
dp = updater.dispatcher
# Define command handlers
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
# Define message handlers
dp.add_handler(MessageHandler(Filters.photo, catify))
# Log all errors
dp.add_error_handler(error)
# Start the bot
updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN)
updater.bot.setWebhook("https://catify-my-face-bot.herokuapp.com/" + TOKEN)
# Run the bot until Ctrl-C is pressed or the process receives SIGINT, SIGTERM or SIGABRT
updater.idle()
if __name__ == "__main__":
main()