forked from dingdang-robot/dingdang-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendMessage.py
51 lines (47 loc) · 2.06 KB
/
SendMessage.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
# -*- coding: utf-8-*-
# 向微信好友发消息插件
import logging
import sys
import re
import time
reload(sys)
sys.setdefaultencoding('utf8')
WORDS = ["FAXIN"]
SLUG = "send_message"
def handle(text, mic, profile, wxbot=None):
logger = logging.getLogger(__name__)
try:
text_utf8 = text.decode('utf8')
# 匹配规则:[给|向] XXX(微信昵称) [发信|发信息|发送信息|发送消息|发消息] 说 XXX(消息内容)
# 微信昵称 -- 中文,名称2-15位长度; 消息内容 -- 1-25位长度
PATTERN = ur'(给|向)([\u4e00-\u9fa5]{2,15})(发信|发送|发消息)(\S+)(说)(\S+)'
pattern = re.compile(PATTERN)
m = pattern.search(text_utf8)
if not m or m.lastindex < 3:
mic.say('抱歉,没能识别联系人,请重试', cache=True)
return;
username = m.group(2)
mic.say('好嘞,开始给%s送信' % (username), cache=True)
time.sleep(.3)
if m.lastindex < 6:
mic.say('抱歉,没有听清楚消息内容', cache=True)
return;
msgbody = m.group(6)
comfirm_input = u'确认'
confirm_message_body = True
if SLUG in profile:
if 'confirm_message_body' in profile[SLUG]:
confirm_message_body = profile[SLUG]['confirm_message_body']
if confirm_message_body:
mic.say('将要提交消息,消息内容是:%s,请在滴一声后确认' % (msgbody))
comfirm_input = mic.activeListen(MUSIC=True)
if comfirm_input is not None and any(word in comfirm_input for word in [u"确认", u"好", u"是", u"OK"]):
wxbot.send_msg(username, msgbody, False)
mic.say('提交成功,消息内容:%s' % (msgbody), cache=True)
return
mic.say('确认失败,操作已取消,请重新尝试', cache=True)
except Exception, e:
logger.error(e)
mic.say('抱歉,消息没有提交成功', cache=True)
def isValid(text):
return any(word in text for word in [u"发信", u"发送", u"发消息"])