用wxBot和图灵机器人API实现微信群聊机器人

时间:2025-02-17 11:12:43
#!/usr/bin/env python # coding: utf-8 from wxbot import * import ConfigParser import json class TulingWXBot(WXBot): def __init__(self): WXBot.__init__(self) self.tuling_key = "" try: cf = () ('') self.tuling_key = ('main', 'key') except Exception: pass print 'tuling_key:', self.tuling_key def tuling_auto_reply(self, uid, msg): if self.tuling_key: url = "http:///openapi/api" user_id = ('@', '')[:30] body = {'key': self.tuling_key, 'info': ('utf8'), 'userid': user_id} r = (url, data=body) respond = () result = '' if respond['code'] == 100000: result = respond['text'].replace('<br>', ' ') elif respond['code'] == 200000: result = respond['url'] else: result = respond['text'].replace('<br>', ' ') return result else: return u"知道啦" def handle_msg_all(self, msg): if msg['msg_type_id'] == 4 and msg['content']['type'] == 0: # text message from contact self.send_msg_by_uid(self.tuling_auto_reply(msg['user']['id'], msg['content']['data']), msg['user']['id']) elif msg['msg_type_id'] == 3: # group message if msg['content']['data'].find('@') >= 0: # someone @ another my_names = self.get_group_member_name(msg['user']['id'], ['UserName']) if my_names is None: my_names = {} if 'NickName' in and len(['NickName']) > 0: my_names['nickname2'] = ['NickName'] if 'RemarkName' in and len(['RemarkName']) > 0: my_names['remark_name2'] = ['RemarkName'] is_at_me = False text_msg = '' for _ in my_names: if msg['content']['data'].find('@'+my_names[_]) >= 0: is_at_me = True text_msg = msg['content']['data'].replace('@'+my_names[_], '').strip() break if is_at_me: # someone @ me snames = self.get_group_member_name(msg['user']['id'], msg['content']['user']['id']) src_name = '' if 'display_name' in snames: src_name = snames['display_name'] elif 'nickname' in snames: src_name = snames['nickname'] elif 'remark_name' in snames: src_name = snames['remark_name'] if src_name != '': reply = '@' + src_name + ' ' if msg['content']['type'] == 0: # text message reply += self.tuling_auto_reply(msg['content']['user']['id'], text_msg) else: reply += u"对不起,只认字,其他杂七杂八的我都不认识,,,Ծ‸Ծ,," self.send_msg_by_uid(reply, msg['user']['id']) def main(): bot = TulingWXBot() = True ['qr'] = 'png' () if __name__ == '__main__': main()