From d7c4f7e14c3b4e0bfba5991c47cab2587a5ab384 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Tue, 22 Jan 2019 16:54:57 +0530 Subject: [PATCH] Complete bot.py. Add groups to config. Change /post to POST. Update gitignore. --- .gitignore | 1 + bot/bot.py | 16 +++++++++++++++- bot/sample.config.py | 3 +++ bot/server.py | 2 +- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index e61bca2..b907f97 100644 --- a/.gitignore +++ b/.gitignore @@ -114,3 +114,4 @@ dmypy.json # Pyre type checker .pyre/ +config.py diff --git a/bot/bot.py b/bot/bot.py index 6572240..9f0d0ad 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -1,13 +1,27 @@ #!/usr/bin/env python3 import config +import logging +import requests from telegram.ext import Updater, MessageHandler, Filters +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - \ + %(message)s', level=logging.INFO) + def message_handling(bot, update): - pass + print("in here") + if update.message.chat.id in config.groups: + print("in here") + for i in update.message.text.split("\n"): + params = {"message": (f"TG - {update.message.from_user.first_name}" + f": {i}")} + requests.post("http://localhost:" + str(config.port) + "/post", + data=params) + print(params) updater = Updater(token=config.api_key) dispatcher = updater.dispatcher dispatcher.add_handler(MessageHandler(Filters.text, message_handling)) +updater.start_polling() diff --git a/bot/sample.config.py b/bot/sample.config.py index 92c1f8b..cee387e 100644 --- a/bot/sample.config.py +++ b/bot/sample.config.py @@ -4,3 +4,6 @@ api_key = "123456789:xxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Port to connect to server port = 9876 + +# Groups to read from +groups = [] diff --git a/bot/server.py b/bot/server.py index 52a0f15..1bf62bf 100644 --- a/bot/server.py +++ b/bot/server.py @@ -21,4 +21,4 @@ with open('schema.sql') as fp: cursor.executescript(fp.read()) app.add_url_rule('/get', 'get', lambda: get(db)) -app.add_url_rule('/post', 'post', lambda: post(db)) +app.add_url_rule('/post', 'post', lambda: post(db), methods=["POST"])