def handleHelp(self, income, sender, gid): u"""显示帮助信息.""" words = [] funcs = [] for k, v in self._answer_map.items(): if isinstance(v, (unicode, str, basestring, msgfmt.Message)): words.append(k) elif callable(v): funcs.append((k, v)) funcs_doc = [u"%s -> %s" % (name, func.__doc__) for name, func in funcs] doc = u"""====聊天机器人帮助==== 直接对话就可以激活我。群聊天请使用Hi的回复功能或者 @ 我。 {匹配回复}: %s {功能调用}: %s""" % (u', '.join(words[::-1]), u'\n'.join(funcs_doc)) msg = msgfmt.Message(fontname=u'黑体', bold=False, size=11, color=0x3C1FD0) #msg.face(u"花痴") msg.cface('b80439620592368b0874e7a7b3442059', 'jpg') msg.text(u'你好, 我是小丫~~~') msg.text(u'\n') #msg.face(u"疑问") msg.text(doc) return msg
def getAnswerByQuestion(self, income, sender, gid=None): time.sleep(0.1) # sync wait ret = None stripped_income = income.replace(u'@' + unicode(self.username, __encoding__), '').strip().lower() for keyword in self._answer_map: #if keyword in income.lower(): if stripped_income.startswith(keyword): ret = self._answer_map[keyword] # if is a function if callable(ret): # func(income:unicode, sender:unicode, gid:int) ret = ret(income, sender, gid, ) break if ret is None: if callable(self._default_handler): return self._default_handler(income, sender, gid) return None # a group msg, but not to me if gid and (u'@' + self.username) not in income: return None # if already a Message Object, directy return if isinstance(ret, msgfmt.Message): return ret msg = msgfmt.Message(fontname=u'黑体', bold=False, size=11, color=0x6B4C3F) if gid: msg.reply(sender, income.replace('\n', ' ').replace('\r', '')) msg.text(u'\n') msg.text(ret) return msg
def do_about(income, sender, gid): u"""显示机器人关于信息.""" msg = msgfmt.Message(fontname=u'黑体', bold=False, size=11, color=0x3C1FD0) msg.cface('b80439620592368b0874e7a7b3442059', 'jpg') msg.text(u"关于小丫……") msg.text(u'\n') msg.text(u'''姓名:小丫 性别:不祥 年龄:不祥 爱好:瞎扯淡 协议:WebHi 版本:v0.2 作者:andelf''') return msg