From d067e46b486b8cfbefa62249e7e375c0e60c110b Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Wed, 10 Jun 2020 23:12:58 +0200 Subject: [PATCH] initial --- .gitignore | 2 + README.md | 22 +++++++++++ telegram-interface.py | 89 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 telegram-interface.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b11ae8a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.token +connected_chat_ids.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..06d64e6 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# HTTP->Telegram Gateway Notification Service +Simplistic server that connect to a Telegram bot, takes messages via *POST*-requests containing json encoded data and sends them to all clients that are subscribed + +# Telegram Setup +- create a bot as described [here](https://core.telegram.org/bots) +- add your bot as a contact + +# Telegram Bot Commands + + /start # subscribe + /unsubscribe # unsubscribe + +# Server Setup + + usage: telegram-interface.py [-h] [--interface INTERFACE] [--port PORT] + [--token-file TOKEN_FILE] + + optional arguments: + -h, --help show this help message and exit + --interface INTERFACE Interface on which to listen (default: localhost) + --port PORT Port on which to listen (default: 5000) + --token-file TOKEN_FILE File containing the Bot-Token (default: bot.token) diff --git a/telegram-interface.py b/telegram-interface.py new file mode 100755 index 0000000..f2dece4 --- /dev/null +++ b/telegram-interface.py @@ -0,0 +1,89 @@ +#!/usr/bin/python3 + +import time +import argparse +import telegram.ext as tg +import logging +import flask + +HOST = "icinga.atlantishq.de" +CHAT_ID_FILE = "connected_chat_ids.txt" +app = flask.Flask("Telegram Notification Gateway") +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) + +def dbWriteChatId(chatId): + saves = [] + with open(CHAT_ID_FILE, "r") as f: + for lines in f: + saves += [int(lines)] + saves += [chatId] + saves = set(saves) + with open(CHAT_ID_FILE, "w") as f: + for s in saves: + f.write(str(s) + "\n") + +def dbRemoveChatId(chatId): + saves = [] + with open(CHAT_ID_FILE, "r") as f: + for lines in f: + saves += [int(lines)] + saves.remove(chatId) + saves = set(saves) + with open(CHAT_ID_FILE, "w") as f: + for s in saves: + f.write(str(s) + "\n") + +def dbReadChatIdFile(): + saves = [] + with open(CHAT_ID_FILE, "r") as f: + for line in f: + saves += [int(line)] + return saves + +def sendMessageToAllClients(msg): + for chatId in dbReadChatIdFile(): + chatId = int(chatId) + updater.bot.send_message(chat_id=chatId, text=msg) + + +def startHandler(update, context): + startText = "Icinga Bot connected and listening on {}".format(HOST) + dbWriteChatId(update.effective_chat.id) + context.bot.send_message(chat_id=update.effective_chat.id, text=startText) + +def unsubscribeHandler(update, context): + text = "Unsubscribed. You won't receive anymore messages." + dbRemoveChatId(update.effective_chat.id) + context.bot.send_message(chat_id=update.effective_chat.id, text=text) + + +@app.route('/send-all', methods=["POST"]) +def sendToAll(): + sendMessageToAllClients(flask.request.json["message"]) + return ("","204") + +if __name__ == "__main__": + + parser = argparse.ArgumentParser(description='Simple Telegram Notification Interface', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument('--interface', default="localhost", help='Interface on which to listen') + parser.add_argument('--port', default="5000", help='Port on which to listen') + parser.add_argument("--token-file", default="bot.token", help="File containing the Bot-Token") + args = parser.parse_args() + + # create bot # + updater = None + with open(args.token_file, 'r') as f: + updater = tg.Updater(token=f.read().strip("\n"), use_context=True) + + # add command handlers # + updater.dispatcher.add_handler(tg.CommandHandler('start', startHandler)) + updater.dispatcher.add_handler(tg.CommandHandler('restart', startHandler)) + updater.dispatcher.add_handler(tg.CommandHandler('unsubscribe', unsubscribeHandler)) + + # run bot # + updater.start_polling() + + # run flask server # + app.run(host=args.interface, port=args.port) +