This commit is contained in:
Yannik Schmidt
2020-06-10 23:12:58 +02:00
commit d067e46b48
3 changed files with 113 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
*.token
connected_chat_ids.txt

22
README.md Normal file
View File

@@ -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)

89
telegram-interface.py Executable file
View File

@@ -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)