微信人工智障聊天机器人创建教程

来源:互联网 发布:淘宝第三方服务退款 编辑:程序博客网 时间:2024/04/30 06:08

    通过python的itchat库调用图灵机器人的接口,可以实现人机自动对话.我是在ubuntu16.04下进行开发的.

    首先 进入终端,输入pip install itchat安装库

    在图灵机器人注册帐号获得 key 值  http://www.tuling123.com/

    在sublime-text 里面编写程序 运行.扫描二维码登录.  简短代码如下,实现自动回复功能.

# coding:utf-8import itchatfrom itchat.content import TEXTfrom itchat.content import *import sysimport timeimport reimport requestsreload(sys)sys.setdefaultencoding('utf8')import oskey = 'ec55df8368c945febe7727031bd71346'msg_information = {}face_bug=None  #针对表情包的内容def get_response(msg):    apiUrl = 'http://www.tuling123.com/openapi/api'    data={    'key'   : key,    'info'  : msg,    'userid':'杨方健的智能小助手'    }    try:        r = requests.post(apiUrl,data=data).json()        return r.get('text')    except:      return@itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO],isFriendChat=True, isGroupChat=True, isMpChat=True)def tuling_reply(msg):    defaultReply = 'i received'+msg['Text']    reply = get_response(msg['Text'])    return reply or defaultReplyitchat.auto_login(hotReload=True)itchat.run()


原创粉丝点击