diff options
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Procfile | 1 | ||||
| -rw-r--r-- | requirements.txt | 4 | ||||
| -rw-r--r-- | runtime.txt | 1 | ||||
| -rw-r--r-- | taulu.png | bin | 0 -> 370904 bytes | |||
| -rw-r--r-- | taulubot.py | 90 |
6 files changed, 98 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a8515b8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +photo +run_bot.bat diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..3e00c95 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: python taulubot.py 185486156:AAG2-8O_FtNIYWgg9F0lqMXoE16MO1o1NVE diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0bafeec --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +Pillow==3.1.1 +requests==2.9.1 +telepot==6.3 +virtualenv==14.0.6
\ No newline at end of file diff --git a/runtime.txt b/runtime.txt new file mode 100644 index 0000000..294a23e --- /dev/null +++ b/runtime.txt @@ -0,0 +1 @@ +python-3.5.0 diff --git a/taulu.png b/taulu.png Binary files differnew file mode 100644 index 0000000..19856c1 --- /dev/null +++ b/taulu.png diff --git a/taulubot.py b/taulubot.py new file mode 100644 index 0000000..83fc4c3 --- /dev/null +++ b/taulubot.py @@ -0,0 +1,90 @@ +import sys +import time +from pprint import pprint +import telepot +import urllib.request +import time +from PIL import Image +import os + +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) + +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"] + + 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) + + if photos["total_count"] == 0: + bot.sendMessage(chat_id, "Vitun taulu, hommaa kuva!") + return True + + print("pic count: {}".format(photos["total_count"])) + + pic = photos["photos"][0][-1] + file_id = pic["file_id"] + + if not os.path.exists("photo/"): + os.makedirs("photo/") + + filepath = "photo/" + file_id + ".jpg" + bot.downloadFile(file_id, filepath) + time.sleep(2) + + im = Image.open(filepath) + im.thumbnail(img_size, Image.ANTIALIAS) + im.paste(taulu_im, (0, 0), taulu_im) + im.save(final_path) + + time.sleep(2) + f = open(final_path, "rb") + print("Sending photo {} to chat {}...".format(final_path, chat_id)) + bot.sendPhoto(chat_id, f) + + return True + +# Getting the token from command-line is better than embedding it in code, +# because tokens are supposed to be kept secret. +TOKEN = sys.argv[1] + +bot = telepot.Bot(TOKEN) +print('Listening ...') + +# Keep the program running. +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"] + + except Exception as e: + print("Exception! Details: {}".format(e)) + |
