-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
80 lines (65 loc) · 3.15 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
import logging
import random
import sys
import os
import config
import radarcheck
from PIL import Image
import cv2
from time import sleep
from telegram import ChatAction
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
logging.basicConfig(level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger()
if config.MODE == "dev":
def run(updater):
updater.start_polling()
elif config.MODE == "prod":
def run(updater):
updater.start_webhook(listen="0.0.0.0",
port=config.PORT,
url_path=config.TOKEN)
updater.bot.set_webhook("https://{}.herokuapp.com/{}".format(config.HEROKU_APP_NAME, config.TOKEN))
else:
logger.error("No MODE specified!")
sys.exit(1)
def start_handler(bot, update):
logger.info("User {} started bot".format(update.effective_user["id"]))
update.message.reply_text("Search 'ukbb radar' images in google and send me snapshots of the radar. I will read from image and tell you the direction and speed of the wind")
def random_handler(bot, update):
number = random.randint(0, 10)
logger.info("User {} randomed number {}".format(update.effective_user["id"], number))
update.message.reply_text("Random number: {}".format(number))
def wind_direction_handler(bot, update):
image = radarcheck.UrlToImage(radarcheck.radar_image_url)
wind_dir = radarcheck.GetWindDirection(image)
logger.info("User {} wind direction {}".format(update.effective_user["id"], wind_dir))
update.message.reply_text(wind_dir);
def photo_handler(bot, update):
bot.send_chat_action(chat_id=update.effective_message.chat_id, action=ChatAction.TYPING)
sleep(0.5)
telegram_file = update.message.photo[-1]
logger.info("User {} sent photo".format(update.effective_user["id"]))
if telegram_file.width == 654 and telegram_file.height == 479:
url = telegram_file.get_file().file_path
img = radarcheck.UrlToImage(url)
wind_dir = radarcheck.GetWindDirection(img)
wind_speed = radarcheck.GetWindSpeed(img)
reply = "{}\n{}".format(wind_dir, wind_speed)
logger.info("Reply:\n{}".format(reply))
update.message.reply_text(reply)
else:
update.message.reply_text("Expected image size is 654x479")
logger.info("Unexpected photo size: {} x {}".format(telegram_file.width, telegram_file.height))
def text_handler(bot, update):
logger.info("User {} sends text:\n\n{}".format(update.effective_user["id"], update.message.text))
if __name__ == '__main__':
logger.info("Starting bot")
updater = Updater(config.TOKEN)
updater.dispatcher.add_handler(CommandHandler("wind_direction", wind_direction_handler))
updater.dispatcher.add_handler(CommandHandler("start", start_handler))
updater.dispatcher.add_handler(CommandHandler("random", random_handler))
updater.dispatcher.add_handler(MessageHandler(Filters.photo, photo_handler))
updater.dispatcher.add_handler(MessageHandler(Filters.text, text_handler))
run(updater)