-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
65 lines (51 loc) · 2.11 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
import logging
import time
from threading import Thread
from color_extraction import extract_color
from telegram import *
from telegram.ext import *
from key import api
WELCOME_TEXT = """Welcome to WhatsColor bot!\nTo use this bot please make sure:\n1. Take a photo with the flash on 🔦\n2. Try to isolate the object in the photo\n3. Multi-color object might be inaccurate"""
logging.basicConfig(
format="[%(levelname)s %(asctime)s %(module)s:%(lineno)d] %(message)s",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
# the start command
def start(update: Update, context: CallbackContext):
chat_id = update.effective_chat.id
logger.info(f"> Start chat #{chat_id}")
context.bot.send_message(chat_id=chat_id, text=WELCOME_TEXT)
# the /help command
def help_command(update, context):
update.message.reply_text('You need to upload an image, I will scan it and return the dominant color in that image.')
counter = 0
def receive_image(update, context):
global counter
counter += 1
image = update.message.photo[-1].get_file()
logger.info(f"recived image:#{counter} {image}")
update.message.reply_text('Just a moment I am scanning the image ---->')
Thread(target=process_image, args=(context, counter, image, update)).start()
def process_image(context, counter, image, update):
t0 = time.perf_counter()
result = extract_color(image['file_path'])
t = time.perf_counter() - t0
logger.info(f"result for image:{counter} {result[0]} time={t:.1f}")
update.message.reply_text(result[0])
chat_id = update.message.chat_id
context.bot.send_photo(chat_id=chat_id, photo=result[1])
logger.info(f"sent image:{counter}")
# ----------------- STARTING THE BOT ---------------------
logger.info("Starting up bot...")
updater = Updater(api, use_context=True)
dp = updater.dispatcher
# Commands
dp.add_handler(CommandHandler('start', start))
dp.add_handler(CommandHandler('help', help_command))
dp.add_handler(MessageHandler(Filters.all, receive_image))
# dp.add_handler(MessageHandler(Filters.all, error))
# Run the bot
updater.start_polling()
updater.idle()
logger.info("Bye!")