-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
executable file
·70 lines (57 loc) · 1.97 KB
/
main.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
68
69
70
# -*- coding: utf-8 -*-
import os
import json
import logging
from flask import Flask, request, jsonify
from faces import faces
app = Flask(__name__)
@app.route('/')
def hello():
"""Return a friendly HTTP greeting."""
return "Hello!"
@app.route('/{}'.format(os.environ['TELEGRAM_BOT_TOKEN']), methods=['POST'])
def webhook():
req = request.get_json()
logging.info(req)
inline_query = req.get('inline_query')
message = req.get('message')
if inline_query:
offset = inline_query.get('offset')
offset = 0 if not offset else int(offset)
query = inline_query.get('query')
results = [{
'type': 'article',
'id': str(i + offset),
'title': face,
'description': u'{} {}'.format(query, face) if query else face,
'message_text': u'{} {}'.format(query, face) if query else face,
} for i, face in enumerate(faces[offset:offset + 50])]
resp = {'method': 'answerInlineQuery',
'inline_query_id': inline_query.get('id'),
'results': results,
'next_offset': str(offset + len(results))}
return jsonify(**resp)
elif message:
text = message.get('text')
if text == '/list':
resp = {'method': 'sendMessage',
'text': '\n'.join(faces),
'chat_id': message.get('from').get('id')}
elif text == '/start':
resp = {'method': 'sendMessage',
'text': '''I allow you to append funny ascii faces to your messages.
You can contribute to the code here:
https://github.com/phil-r/asciifacesbot''',
'chat_id': message.get('from').get('id')}
else:
resp = {'method': 'sendMessage',
'text': 'I know only /list command ¯\_(ツ)_/¯',
'chat_id': message.get('from').get('id')}
return jsonify(**resp)
return 'OK'
@app.errorhandler(404)
def page_not_found(e):
return 'Sorry, Nothing at this URL.', 404
@app.errorhandler(500)
def application_error(e):
return 'Sorry, unexpected error: {}'.format(e), 500