Created
July 26, 2018 10:49
-
-
Save hedgeven/6568bd96fce28170a668b3aff52798a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import os | |
import telegram | |
from flask import Flask, request | |
TOKEN = 'XXXXXXXXXXXXXXXXXX' | |
HOST = 'bot.domain.tld' | |
CERT = '/path/to/cert.pem' | |
bot = telegram.Bot(TOKEN) | |
app = Flask(__name__) | |
@app.route('/' + TOKEN, methods=['POST']) | |
def webhook(): | |
update = telegram.update.Update.de_json(request.get_json(force=True)) | |
chat_id = update.message.chat_id | |
if '/start' in update.message.text.encode('utf-8'): | |
text = 'Your Chat ID is: %s' % chat_id | |
bot.sendMessage(chat_id=update.message.chat_id, text=text) | |
return 'Ok' | |
@app.route('/send', methods=['POST']) | |
def index(): | |
if request.method == "POST": | |
req = request.get_json(force=True) | |
chat_id = req['to'] | |
subj = req['subj'] | |
spl_text = subj.split(';') | |
if chat_id: | |
if len(spl_text) == 3: | |
sdict = { 'Not classified': 'Gray', | |
'Information': 'CornflowerBlue', | |
'Warning': 'Orange', | |
'Average': 'OrangeRed', | |
'High': 'Red', | |
'Disaster': 'DarkRed' | |
} | |
if spl_text[1] == 'PROBLEM': | |
text = '<b>%s: %s - %s</b>' % (spl_text[1], spl_text[0], spl_text[2]) | |
else: | |
text = '%s: %s - %s' % (spl_text[1], spl_text[0], spl_text[2]) | |
else: | |
text = subj | |
bot.sendMessage(chat_id=chat_id, text=text, parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True) | |
return 'Ok' | |
@app.route('/') | |
def hello(): | |
return 'Ok' | |
def setWebhook(): | |
bot.setWebhook(webhook_url='https://%s/%s' % (HOST, TOKEN), | |
certificate=open(CERT, 'rb')) | |
if __name__ == '__main__': | |
setWebhook() | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment