python微信机器人之小i

前面介绍了图灵机器人的接入,但是在后面的聊天中发现这个鬼的智能化程度太低了,聊天一点都不好玩,不说和siri比吧,和我以前接触过的小i都没法比,
所以在顺着图灵的做法,把小i也嵌入进去了
微信接口还是用的wxbot这个框架,虽然这个框架不在地,代码变量的设置啊,还有部分功能的不完善,但是目前也懒的去找了,能用就行
小i的接口相对比较麻烦,他有一个header,而且其中还有他自己的加密算法,当然了,一步步的走下去也不麻烦,然而,,python是什么?我怎么可能自己去写呢,,果断google啊,serach:小i机器人api接口。 还真尼玛找到了,在csdn上别人实现的一个方法,然后就没有然后了,
把图灵接入微信的代码直接拷贝过来修改了一下接口,就没有了,
看起来貌似很简单,,,其实也确实简单,接口什么的都已经有了,直接嵌入就好了,而且别人都封装的挺好的了,

def XiaoI():
    test_key = "ytsqcyFYsFou"
    test_sec = "N32kQFnQo12AxaekmwBM"

    signature_ask = xiaoi.ibotcloud.IBotSignature(app_key=test_key,
                                                  app_sec=test_sec,
                                                  uri="/ask.do",
                                                  http_method="POST")

    signature_reg = xiaoi.ibotcloud.IBotSignature(app_key=test_key,
                                                  app_sec=test_sec,
                                                  uri="/recog.do",
                                                  http_method="POST")

    signature_tts = xiaoi.ibotcloud.IBotSignature(app_key=test_key,
                                                  app_sec=test_sec,
                                                  uri="/synth.do",
                                                  http_method="POST")

    params_tts = xiaoi.ibotcloud.TTSParams(url="http://vcloud.xiaoi.com/synth.do")
    params_reg = xiaoi.ibotcloud.RegParams(url="http://vcloud.xiaoi.com/recog.do")
    params_ask = xiaoi.ibotcloud.AskParams(platform="custom",
                                           user_id="abc",
                                           url="http://nlp.xiaoi.com/ask.do",
                                           response_format="xml")

    ask_session = xiaoi.ibotcloud.AskSession(signature_ask, params_ask)
    return ask_session
    #reg_session = xiaoi.ibotcloud.RegSession(signature_reg, params_reg)
    #tts_session = xiaoi.ibotcloud.TTSSession(signature_tts, params_tts)


def getanswer(text):
    return XiaoI().get_answer(text).http_body#.encode('gbk')

我在后面加了一个get函数,算是提取了一个接口吧,

想了会,
还是把微信和机器人的接口代码放上来吧

tulinoc = TuLin()
class MyWXBot(WXBot):
    def handle_msg_all(self, msg):
        if msg['msg_type_id'] == 4 and msg['content']['type'] == 0:
            tulinoc.sendTextToTuLin(msg['content']['data'])
            self.send_msg_by_uid(tulinoc.getReturnText(), msg['user']['id'])
        if msg['msg_type_id'] == 3 and msg['content']['type'] == 0:
            tulinoc.sendTextToTuLin(msg['content']['desc'])
            self.send_msg_by_uid(tulinoc.getReturnText(), msg['user']['id'])
        if msg['msg_type_id'] == 5 and msg['content']['type'] == 0:
            tulinoc.sendTextToTuLin(msg['content']['data'])
            self.send_msg_by_uid(tulinoc.getReturnText(), msg['user']['id'])
    def getUserID(self):
        CityinwuhanID = []
        userCityName = u'徐州'
        for user in self.contact_list:
            if userCityName == user['City'].encode('utf-8'):
                CityinwuhanID.append(self.get_user_id(user['NickName']))
            #else:
                #print "city:%s" %user['City'].encode('utf-8') 
        return CityinwuhanID        
    ''' def schedule(self): userIDList = self.getUserID() for userID in userIDList: self.send_msg_by_uid('你好,我是图灵,一个自动化机器人,收到请不要回复,打扰了',userID) self.send_msg(u'7小媛',self.get_contact_name(userID)['nickname'].encode('utf-8')) time.sleep(10) '''
def main():    
    bot = MyWXBot()
    bot.DEBUG = True
    bot.run()
if __name__ == '__main__':
    main()

你可能感兴趣的:(python,微信,小i)