-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.py
101 lines (88 loc) · 4.03 KB
/
receiver.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import requests, threading, config, urllib.request, time, os, util, log, re
from PIL import Image
class Receiver:
def __init__(self, username, conversation_id, replier):
self.conversation_id = conversation_id
self.replier = replier
self.username = username
self.thread_stop_flag = False
self.thread = threading.Thread(target=lambda: self.listen())
self.watermark = None
self.start_listening()
def url(self):
return "https://directline.botframework.com/v3/directline/conversations/%s/activities%s" % (self.conversation_id, "?watermark=" + self.watermark if self.watermark else "")
def listen(self):
while not self.thread_stop_flag:
url = self.url()
res = requests.get(url, headers=util.auth_header()).json()
self.watermark = res.get("watermark")
for activity in res["activities"]:
if activity["from"]["id"] != self.username:
self.reply_text(activity["text"])
attachments = activity.get("attachments")
if attachments:
for attachment in attachments:
content_type = attachment["contentType"]
if content_type.startswith("image/"):
ext = content_type.split("/")[1]
url = attachment["contentUrl"]
self.reply_pic(url, ext)
elif content_type == "application/vnd.microsoft.card.hero":
self.reply_hero_card(attachment)
suggested_actions = activity.get("suggestedActions")
if suggested_actions:
self.reply_actions(suggested_actions["actions"], "推荐(请输入括号内的字符串):")
time.sleep(config.poll_interval)
def reply_text(self, text):
img_re = "!\[.*\]\(.*\)"
s = re.search(img_re, text)
log.info(text)
if s:
real_addr = s.group(0).split("(")[1]
real_addr = real_addr[0:len(real_addr)-1]
prior = text[0:s.start(0)]
self.replier.text(prior)
log.info(prior)
self.reply_pic(real_addr)
self.reply_text(text[s.end(0):])
elif text.strip():
log.info(text)
self.replier.text(text.strip())
def reply_hero_card(self, content):
card = content["content"]
title_str = util.or_empty(card.get("title"))
if title_str:
title_str += '\n\n'
title_str += util.or_empty(card.get("text"))
self.replier.text(title_str)
if card.get("image"):
for image in card.get("image"):
self.reply_pic(image["url"])
if len(card["buttons"]) > 0:
self.reply_actions(card["buttons"], "以下选项可用(请输入括号内的字符串):")
def reply_actions(self, actions, prompt):
action_str = prompt + '\n'
for action in actions:
if action["type"] == 'imBack':
title = action["title"]
if title == "Yes":
action_str += "· 是(Yes)"
elif title == "No":
action_str += "· 不(No)"
else:
action_str += "· %s(%s)" % (action["title"], action["value"])
action_str += "\n"
elif action["type"] == 'openUrl':
action_str += '· %s(%s)\n' % (action["title"], action["value"])
self.replier.text(action_str)
def reply_pic(self, pic_url, ext):
log.info("接收到图片,地址:" + pic_url)
filename = "%s_%s.%s" % (time.time(), self.conversation_id, ext)
util.download_file(pic_url, filename)
self.replier.pic(filename)
os.remove(filename)
def start_listening(self):
self.thread.setDaemon(True)
self.thread.start()
def dispose(self):
self.thread_stop_flag = True