-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhowdoibot.py
67 lines (55 loc) · 1.92 KB
/
howdoibot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask import Flask, request
import telepot
import urllib3
import json
from howdoi.howdoi import howdoi
from path import Path
from tinydb import TinyDB, Query
from datetime import timedelta, datetime
db = TinyDB('messages.json')
config_path = Path(__file__).parent / 'config.json'
info = json.load(open(config_path))
if 'pythonanywhere' in info['URL']:
proxy_url = "http://proxy.server:3128"
telepot.api._pools = {
'default': urllib3.ProxyManager(proxy_url=proxy_url, num_pools=3, maxsize=10, retries=False, timeout=30),
}
telepot.api._onetime_pool_spec = (urllib3.ProxyManager, dict(
proxy_url=proxy_url, num_pools=1, maxsize=1, retries=False, timeout=30))
CERT = 'server.crt'
CERT_KEY = 'server.key'
bot = telepot.Bot('{AUTHORIZATION_TOKEN}'.format(
**info))
bot.setWebhook(
"https://{URL}/{SECRET_NUMBER}".format(**info), max_connections=1, certificate=open(CERT, 'rb'))
app = Flask(__name__)
secret = "{SECRET_NUMBER}".format(**info)
context = (CERT, CERT_KEY)
howdoi_args = {
'num_answers': 1,
'pos': 1,
'all': False,
'color': False,
}
@app.route('/{}'.format(secret), methods=["POST"])
def telegram_webhook():
update = request.get_json()
app.logger.info(update)
msg = Query()
if "message" in update and timedelta(minutes=5) > (datetime.now() - datetime.fromtimestamp(update['message']['date'])):
db.insert(update['message'])
text = update["message"]["text"]
args = {
'query': text.split(' ')
}
args.update(howdoi_args)
text = howdoi(args)
chat_id = update["message"]["chat"]["id"]
try:
bot.sendMessage(chat_id, "```\n{}```".format(text), parse_mode='Markdown')
except telepot.exception.BotWasBlockedError as e:
app.logger.error(str(e))
return "OK"
if __name__ == "__main__":
print(info)
app.run(port=5000, ssl_context=context, debug=True, host='0.0.0.0')