1
0
mirror of https://github.com/Adam-Ant/QuotesDB synced 2024-12-20 11:34:35 +00:00

Add telegram notification support

This commit is contained in:
Adam Dodman 2017-10-20 21:19:16 +01:00
parent 4e410616ad
commit 4f1762e749

26
main.py
View File

@ -6,6 +6,7 @@ from os.path import isfile as isfile
import pymysql import pymysql
from flask import Flask, render_template, session, redirect, url_for, request, flash, abort from flask import Flask, render_template, session, redirect, url_for, request, flash, abort
from passlib.context import CryptContext from passlib.context import CryptContext
import urllib.request
import configparser import configparser
import argparse import argparse
@ -24,6 +25,14 @@ defaultconfig = '''[connection]
password = password =
# Database to use for the application (Default: QuoteDB) # Database to use for the application (Default: QuoteDB)
#database = QuoteDB #database = QuoteDB
[telegram]
# Enable the telegram notifier, sends a message to a specific telegram chat (Default: False)
#enabled = True
# API Token of the Bot
#token =
# ID of the User/Group to message
#id=
''' '''
# Thank you based StackOverflow # Thank you based StackOverflow
@ -102,6 +111,7 @@ def do_user_login(user, password):
@app.route("/") @app.route("/")
def index(): def index():
print()
return gen_page("index.html") return gen_page("index.html")
@app.route("/quotes") @app.route("/quotes")
@ -231,6 +241,12 @@ def addquote():
else: else:
contextin = "\'" + contextin + "\'" contextin = "\'" + contextin + "\'"
if telegramEnabled:
try:
with urllib.request.urlopen('https://api.telegram.org/bot%s/sendmessage?parse_mode=Markdown&chat_id=%i&text=*%s just added a quote to the database:*%%0A %s - _%s_' % (telegramToken, telegramChatid, session['username'], quotein, utility_processor()['uid_to_user'](userin))) as urlreq:
urlreq.read()
except urllib.error.HTTPError:
print("Warning: Telegram notify error!")
sql = "INSERT INTO `Quotes` (`id`, `quote`, `date`, `user`, `context`, `addedby`) VALUES (NULL, '%s', CURRENT_TIMESTAMP, %d, %s, %s);" % (quotein, userin, contextin, session['uid']) sql = "INSERT INTO `Quotes` (`id`, `quote`, `date`, `user`, `context`, `addedby`) VALUES (NULL, '%s', CURRENT_TIMESTAMP, %d, %s, %s);" % (quotein, userin, contextin, session['uid'])
@ -285,6 +301,16 @@ if __name__ == "__main__":
dbpass = config['connection'].get('password') dbpass = config['connection'].get('password')
dbname = config['connection'].get('database', 'QuoteDB') dbname = config['connection'].get('database', 'QuoteDB')
telegramEnabled = config['telegram'].getboolean('enabled', False)
if telegramEnabled:
telegramToken = config['telegram'].get('token')
telegramChatid = config['telegram'].getint('id')
if telegramEnabled and ((telegramToken == '') or (telegramChatid == '')):
print("Error! Telegram enabled but token or chat ID not set. Please check your config and retry")
exit(1)
if dbpass == '': if dbpass == '':
print("Error! Could not read config file. Please ensure it exists and is readable, and retry") print("Error! Could not read config file. Please ensure it exists and is readable, and retry")
exit(1) exit(1)