-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinline.py
82 lines (63 loc) · 2.68 KB
/
inline.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
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
import os
import time
import uuid
import asyncio
import telepot
import urllib3
import telepot.api
import telepot.aio
import hangoutLinker
telepot.api._pools={
'default': urllib3.PoolManager(num_pools=3, maxsize=10, retries=3, timeout=30)
}
token = os.getenv('TELEHANGBOT_TELEGRAM_TOKEN')
commands = [u'/потрындеть',u'/перетереть',u'/takeacall',u'/tac',u'/попиздеть',u'/поговорить',u'/беседа',u'/переговоры']
link_by_user = dict()
async def on_chat_message(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
print('Chat:', content_type, chat_type, chat_id)
if content_type != 'text':
return
command = msg['text'].lower()
if command in commands:
await bot.sendMessage(chat_id, hangoutLinker.getlink())
async def on_inline_query(msg):
async def compute_answer():
query_id, from_id, query_string = telepot.glance(msg, flavor='inline_query')
query_string = query_string.encode('utf-8').decode()
# print('Inline Query:', query_id, from_id, query_string)
uri = link_by_user.get(from_id)
if uri is None:
#generate link and put in cache to avoid link generation on each query_string
uri = hangoutLinker.getlink()
link_by_user[from_id] = uri
articles = [{
'type': 'article',
'id': str(uuid.uuid4()),
'thumb_url': 'http://zags.interfacetesting.ru/lavrov.jpg',
'title': u'Начать сложные переговоры с шутки: ' + query_string,
'message_text': query_string + '\r\n' + uri
}]
return articles
result = await compute_answer()
await bot.answerInlineQuery(msg['id'], result, 1)
def on_chosen_inline_result(msg):
result_id, from_id, query_string = telepot.glance(msg, flavor='chosen_inline_result')
query_string = query_string.encode('utf-8').decode()
# print('Chosen Inline Result:', result_id, from_id, query_string)
#remove link from cache
if link_by_user.get(from_id) is not None:
link_by_user.pop(from_id)
#take some time to start selenium server
time.sleep(30)
hangoutLinker.sepUpAndLogin()
bot = telepot.aio.Bot(token)
answerer = telepot.aio.helper.Answerer(bot)
loop = asyncio.get_event_loop()
loop.create_task(bot.message_loop({'chat': on_chat_message,
'inline_query': on_inline_query,
'chosen_inline_result': on_chosen_inline_result
}))
print('Listening ...')
loop.run_forever()