diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2016-02-10 01:11:00 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2016-02-10 01:11:00 +0200 |
| commit | 349e74ec9eefdd032b7dd12fccff503e0b279b2d (patch) | |
| tree | f5f0d886ec54cc0c527a6fa9dc3ae56741cc978d | |
| parent | 2d36b462488e04c089eec562b2fdb11ca199fd0f (diff) | |
Big restructure
| -rw-r--r-- | taulubot.py | 80 |
1 files changed, 45 insertions, 35 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<token>/" -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"] + time.sleep(5) + res = bot.getUpdates() - if len(res) > 1: - pprint(res) + if len(res) == 0: + continue + + for r in res: try: - success = handle(res) - if success: - offset_id = res[-1]["update_id"] + 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 - except Exception as e: - print("Exception! Details: {}".format(e)) + 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 |
