From 349e74ec9eefdd032b7dd12fccff503e0b279b2d Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Wed, 10 Feb 2016 01:11:00 +0200 Subject: Big restructure --- taulubot.py | 84 ++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 47 insertions(+), 37 deletions(-) diff --git a/taulubot.py b/taulubot.py index 83fc4c3..1f6c4e5 100644 --- a/taulubot.py +++ b/taulubot.py @@ -6,40 +6,26 @@ import urllib.request import time from PIL import Image import os +import calendar final_path = "photo/final.png" taulu_im = Image.open("taulu.png") -url = "https://api.telegram.org/file/bot/" -offset_id = 0 img_size = (640, 640) +START_TIME = calendar.timegm(time.gmtime()) taulu_im.thumbnail(img_size, Image.ANTIALIAS) -def handle(res): - - msg = res[-1]["message"]["text"] - if not ("/taulu" in msg): - return False - - taulu_res = res[-2] - - print("New activity!") - chat_id = taulu_res["message"]["chat"]["id"] +def run_taulu(chat_id, user_id): - if res[-1]["message"]["chat"]["id"] != chat_id: - print("Chat id clash!") - return False - - taulu_user = taulu_res["message"]["from"]["id"] - print("taulu: {}".format(taulu_res["message"]["from"]["first_name"])) - photos = bot.getUserProfilePhotos(taulu_user) + print("New taulu!") + photos = bot.getUserProfilePhotos(user_id) if photos["total_count"] == 0: + print("No pictures found, sending message...") bot.sendMessage(chat_id, "Vitun taulu, hommaa kuva!") return True - print("pic count: {}".format(photos["total_count"])) - + print("Looking up picture...") pic = photos["photos"][0][-1] file_id = pic["file_id"] @@ -47,19 +33,25 @@ def handle(res): os.makedirs("photo/") filepath = "photo/" + file_id + ".jpg" + print("Downloading picture...") bot.downloadFile(file_id, filepath) time.sleep(2) + print("Downloaded!") + print("Opening image for editing...") im = Image.open(filepath) im.thumbnail(img_size, Image.ANTIALIAS) im.paste(taulu_im, (0, 0), taulu_im) + print("Saving image...") im.save(final_path) - time.sleep(2) + print("Saved to {}!".format(final_path)) + f = open(final_path, "rb") print("Sending photo {} to chat {}...".format(final_path, chat_id)) bot.sendPhoto(chat_id, f) + print("Done. Returning...") return True # Getting the token from command-line is better than embedding it in code, @@ -69,22 +61,40 @@ TOKEN = sys.argv[1] bot = telepot.Bot(TOKEN) print('Listening ...') +last_user_in_chat = {} +done_list = [] + +def is_taulu_command(text): + return "/taulu" in text.strip() + # Keep the program running. +offset_id = 0 while 1: - time.sleep(10) - res = bot.getUpdates(offset=offset_id) - print("update count: {}".format(len(res))) - print("offset_id: {}".format(offset_id)) - if len(res) > 4: - offset_id = res[-4]["update_id"] - - if len(res) > 1: - pprint(res) - try: - success = handle(res) - if success: - offset_id = res[-1]["update_id"] + time.sleep(5) + res = bot.getUpdates() - except Exception as e: - print("Exception! Details: {}".format(e)) + if len(res) == 0: + continue + for r in res: + try: + date = int(r["message"]["date"]) + if (date < START_TIME): + continue + + chat_id = r["message"]["chat"]["id"] + user_id = r["message"]["from"]["id"] + msg_id = r["message"]["message_id"] + text = r["message"]["text"] + + except: + print("Broken text") + continue + + if chat_id in last_user_in_chat: + if is_taulu_command(text) and not msg_id in done_list: + taulu_id = last_user_in_chat[chat_id] + run_taulu(chat_id, taulu_id) + done_list.append(msg_id) + + last_user_in_chat[chat_id] = user_id -- cgit v1.3