【教程】给你的QQ接入免费的ChatGPT机器人
转载请注明出处:小锋学长生活大爆炸[xfxuezhang.cn]
先看效果吧:
支持 群聊中“@机器人 + 内容” 的形式,也支持 私聊中“bot + 内容” 的形式。
开始前,你需要已经有了mirai QQ机器人平台,如何搭建可以看这篇教程(我也是用这个):
万字长文保姆级教你制作自己的多功能QQ机器人_qq机器人搭建
主要先搭一个mirai qq机器人平台,后面接入功能就很方便了,什么群转发、qmsg酱的。所以以上教程非常推荐完成!
另外,流量进出不大的话,服务器用轻量云服务器其实也够用了。
腾讯云2核@G4M一年才108: https://url.cn/pXUtW9f8
{lamp/}
限时特惠: 腾讯云服务器 1年92、3年只要388 !做个智能机器人啥的~
入口: https://url.cn/B4K9jWu0
关于ChatGPT的接口,可以使用官方的API,也可以用这里收集的几个免费的:
【代码】基于Python的免费ChatGPT接口调用测试
文件目录结构如图:
然后,我们在上面教程中的demo.py文件中,进行如下修改:
1、导入包:
from chatgpt_bot import *
from urllib.parse import quote
2、在class QQBot
里添加函数:
def parseMsgForChatGPT(self, data, bind_qq, session):
chatbot = False
data = data[0]
msg_type = data['type']
# 过滤消息类型
if msg_type not in ['GroupMessage', 'FriendMessage']:
return
msg_content = ''
for chain in data['messageChain']:
if chain['type']=='At' and str(chain['target'])==bind_qq:
print('>> 群消息,调用ChatGPT')
chatbot = True
elif chain['type'] == 'Plain':
msg_content = chain['text'].strip()
sender_id = data['sender']['group']['id'] if msg_type=='GroupMessage' else data['sender']['id']
sender_name = data['sender']['memberName'] if msg_type=='GroupMessage' else data['sender']['nickname']
if msg_type=='FriendMessage' and msg_content[:3]=='bot':
msg_content = msg_content[3:].strip()
print('>> 好友消息,调用ChatGPT')
chatbot = True
if chatbot:
print('>> 问题为:', msg_content)
msg_resp = None
try:
msg_resp = chatgpt_bot3(quote(msg_content, 'utf-8'))
except:
pass
print('>> ChatGPT回复:', msg_resp)
msg_resp = msg_resp or '<<接口调用异常,请稍后再试>>'
msg_resp = f'@{sender_name}:\n{msg_resp}'
self.sendMsgToGroupSimple(session, sender_id, msg_resp) if msg_type=='GroupMessage' else self.sendFriendMessage(session, sender_id, msg_resp)
def sendMsgToGroupSimple(self, session, group, msg):
data = {
"sessionKey": session,
"group": group,
"messageChain": [{"type": 'Plain', "text": msg}]
}
logger.DebugLog(">> 消息内容:" + str(data))
url = self.addr + 'sendGroupMessage'
try:
res = requests.post(url, data=json.dumps(data)).json()
except:
logger.DebugLog(">> 发送失败")
return 0
logger.DebugLog(">> 请求返回:" + str(res))
if res['code'] == 0:
return res['messageId']
return 0
3、修改qqTransfer
函数:
bot.parseMsgForChatGPT(data, bind_qq, session)
修改完毕,启动即可。
{!{data:image/webp;base64,UklGRlITAABXRUJQVlA4WAoAAAAQAAAADQEAKwEAQUxQSHASAAAB8Mf//3op8f89lG6klVQY0oKhawcHhxRJCwxgGbo7PRayNEinwdLdPTQySyjN0g1Dx9Bw++Oc83zFeT5fZ99pREyA/OX/v/z/l///8v9/G0yf551OOcO+/B2W/fbPlTuPXnmM/6K6ScK3oq1mnsf6rRHR4VjxjnPPY/HeuWMJT/Hf9+vHYVXZznPPE/Thzh29P69VoVjONCIiebpswf9Fr3Cpxdi55wl6ZUHv9zOK5bd+PAmsDYsydLtI0GNTWxQX29vD09zhT/RsAscPr59bnI0DX5iTqech/K9tik4tzreBI2FNzMxn+C/+NIm4MmsCvB+2ZO2xB/9rg0uKa8fCH2FKtan38d/y9/Ti4uZwKxzJ0WEbASdVFnd3gOXhR5a4m/if7JJDXBwVJSILoG7Y0fwu/itqiosjvjsLPpHrkD3MqLQU4PGw/OLe1K224e+T92CfhJXZhgKcGyMu/mDGA/z3+UR+gJFhRexxIKG7uLfED0fwvzmumojILmgcRvxtBcDkCHFrltjVBFzyeXrxTwdEhA2vDgfYXFvc+sluAh7uHiFBe8BdCRdbnQIudxGXlh1/A/9nkyqIYtJ/wcow4b1VABPyiytzdN5OwIQvkohyTziSNizINQpgYw1xZc+Vd/Hf2+8tsRiRAF9KONj2DHCho7jxtb6X8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