This repository has been archived by the owner on Apr 12, 2021. It is now read-only.
forked from nickoala/telepot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanswerer_global.py
54 lines (37 loc) · 1.51 KB
/
answerer_global.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
import sys
import time
import threading
import telepot
"""
$ python2.7 answerer_global.py <token>
Use Answerer globally.
In response to an inline query, it echoes the query string in the returned article.
You can easily check that the latest answer is the one displayed.
"""
def handle(msg):
flavor = telepot.flavor(msg)
if flavor == 'inline_query':
# Just dump inline query to answerer
answerer.answer(msg)
elif flavor == 'chosen_inline_result':
result_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
print 'Chosen Inline Result:', result_id, from_id, query_string
def compute_answer(inline_query):
query_id, from_id, query_string = telepot.glance(inline_query, flavor='inline_query')
print '%s: Computing for: %s' % (threading.current_thread().name, query_string)
articles = [{'type': 'article',
'id': 'abc', 'title': query_string, 'message_text': query_string}]
return articles
# You may control positional arguments to bot.answerInlineQuery() by returning a tuple
# return (articles, 60)
# You may control keyword arguments to bot.answerInlineQuery() by returning a dict
# return {'results': articles, 'cache_time': 60}
TOKEN = sys.argv[1] # get token from command-line
bot = telepot.Bot(TOKEN)
# Create the Answerer, give it the compute function.
answerer = telepot.helper.Answerer(bot, compute_answer)
bot.notifyOnMessage(handle)
print 'Listening ...'
# Keep the program running.
while 1:
time.sleep(10)