完整源代码(以及.py下载链接)见最后!
聊天机器人(Chatterbot)是经由对话或文字进行交谈的计算机程序。能够模拟人类对话,通过图灵测试,如Siri、小爱同学、微软小冰等。
本教程将教你用Python实现4大免费且好用的聊天机器人:**微软小冰、图灵机器人、腾讯闲聊、青云客机器人**!
从简单开始!
青云客机器人
image
简介
完全免费,支持功能:天气、翻译、藏头诗、笑话、歌词、计算、域名信息/备案/收录查询、IP查询、手机号码归属、人工智能聊天
不用注册,不用申请key,拿来就用!
实现
def qingyunke(msg):
输出
原话>> 我好看吗
图灵机器人
image.png
简介
** 图灵机器人**致力于“让机器理解世界”,产品服务包括机器人开放平台、机器人OS和场景方案。通过图灵机器人,开发者和厂商能够以高效的方式创建专属的聊天机器人、客服机器人、领域对话问答机器人、儿童/服务机器人等。
需要注册账号,可以申请5个机器人,未认证账户每个机器人只能回3条/天,认证账户每个机器人能用100条/天。
申请
登录后申请创建机器人,填入相关信息
image
可以接入微信个人号、微信公众号、QQ个人号、API调用
image
在拓展中心开启拓展功能
image
拿到API key,注意下面的密钥不要打开!
image
实现
def tuling(msg):
输出
原话>> 我好看吗
微软小冰
image
简介
微软小冰是领先的跨平台人工智能机器人。微软小冰注重人工智能在拟合人类情商维度的发展,强调人工智能情商,而非任务完成在人机交互中的基础价值。
需要先**领养小冰**!通过**微博**关注**微软小冰**,并向她发送一条消息,点击她回的链接进行领养。
image
领养完成后,按F12打开调试窗口,在Cookie界面找到SUB值(非常重要)
image
随便发一条消息给小冰,在head界面找到source值和uid值(非常重要)
image
注意微博不能手动点注销退出,不然上面获取的值就失效了!****!(非常重要)
实现
def xiaobing(msg):
输出
原话>> 我好看吗
腾讯闲聊
image
简介
腾讯闲聊服务基于AI Lab领先的NLP引擎能力、数据运算能力和千亿级互联网语料数据的支持,同时集成了广泛的知识问答能力,可实现上百种自定义属性配置,以及男、女不同的语言风格及说话方式,从而让聊天变得更睿智、简单和有趣。
需要注册和申请,还需要加密处理。
这里有个坑,**自定义配置闲聊画像**千万不能开启,不然调用总出错,我排查了很久!!!
申请
先创建应用
image
拿到ID和KEY
image
实现
def tencent(msg):
输出
原话>> 我好看吗
完整源码
import requests
import urllib
import time
import json
import string
import random
import urllib
import hashlib
import base64
class Robot:
def __init__(self):
pass
def qingyunke(self, msg):
url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(urllib.parse.quote(msg))
html = requests.get(url)
return html.json()["content"]
def tuling(self, msg):
index = 0
while True:
api_key = [
"618bd2a9b7c6414ebbda21585f0d0752", # 提供一下我的
]
url = 'http://openapi.tuling123.com/openapi/api/v2'
data = {
"perception": {
"inputText": {
"text": msg
},
},
"userInfo": {
"apiKey": api_key[index],
"userId": "1"
}
}
datas = json.dumps(data)
html = requests.post(url, datas).json()
if html['intent']['code'] == 4003:
print(">> 次数用完")
index += 1
if index == len(api_key):
return None
else:
print(">> 换key重试:", index)
time.sleep(1)
continue
break
return html['results'][0]['values']['text']
def tencent(self, msg):
APPID = '2129556483'#!!!!!!!!!!!!这里要改的!!!!!!!!!!!!!!!!!!!!!
APPKEY = ''#!!!!!!!!!!!!这里要改的!!!!!!!!!!!!!!!!!!!!!
url = 'https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat'
params = {
'app_id': APPID,
'time_stamp': str(int(time.time())),
'nonce_str': ''.join(random.choice(string.ascii_letters + string.digits) for x in range(16)),
'session': '10000'.encode('utf-8'),
'question': msg.encode('utf-8')
}
sign_before = ''
for key in sorted(params):
# 键值拼接过程value部分需要URL编码,URL编码算法用大写字母,例如%E8。quote默认大写。
sign_before += '{}={}&'.format(key, urllib.parse.quote(params[key], safe=''))
# 将应用密钥以app_key为键名,拼接到字符串sign_before末尾
sign_before += 'app_key={}'.format(APPKEY)
# 对字符串sign_before进行MD5运算,得到接口请求签名
sign = hashlib.md5(sign_before.encode('UTF-8')).hexdigest().upper()
params['sign'] = sign
# print(params)
html = requests.post(url, data=params).json()
return html['data']['answer']
def sizhi(self, msg):
url = 'https://api.ownthink.com/bot?spoken={}'.format(urllib.parse.quote(msg))
html = requests.get(url)
return html.json()["data"]['info']['text']
def xiaodou(self, msg):
key = 'M1E1OUZJNCtsODZvZndqNj1UV2x4Zz1xWnhvQUFBPT0'
url = 'http://api.douqq.com/?key={}&msg={}'.format(key, msg)
html = requests.get(url)
return html.text
def xiaobing(self, msg):
uid = '5175429989'#!!!!!!!!!!!!这里要改的!!!!!!!!!!!!!!!!!!!!!
source = '209678993'#!!!!!!!!!!!!这里要改的!!!!!!!!!!!!!!!!!!!!!
SUB = '_2A25zeEw7DeRhGeNG6lQS9SbKyD2IHXVQDDrzrDV8PUNbmtANLWXukW9NS25ufhgLW38wP8yAkpHlxnTZ3CN3dQFy'#!!!!!!!!!!!!这里要改的!!!!!!!!!!!!!!!!!!!!!
url_send = 'https://api.weibo.com/webim/2/direct_messages/new.json'
data = {
'text': msg,
'uid': uid,
'source': source
}
headers = {
'cookie': 'SUB='+SUB,
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',
'Referer': 'https://api.weibo.com/chat/'
}
response = requests.post(url_send, data=data, headers=headers).json()
sendMsg = response['text']
time.sleep(1)
while True:
url_get = 'https://api.weibo.com/webim/2/direct_messages/conversation.json?uid={}&source={}'.format(uid, source)
response = requests.get(url_get, headers=headers).json()
getMsg = response['direct_messages'][0]['text']
if sendMsg == getMsg:
time.sleep(1)
else:
return getMsg
if __name__ == '__main__':
msg = "你好"
print("原话:你好") # 原话:你好
robot = Robot()
print(robot.tuling(msg)) # 你说好就好吧。
print(robot.qingyunke(msg)) # 你好,我就开心了
print(robot.tencent(msg)) # 果然我的魅力无敌,终于把你吸引来了,哈哈哈
print(robot.sizhi(msg)) # 感觉不错。你好吗?
print(robot.xiaodou(msg)) # 你好哇,很开心你能找我聊天。
print(robot.xiaobing(msg)) # (不想登微博了,自行测试吧!)
链接:https://download.csdn.net/download/sxf1061700625/12555760