使用go-cqhttp结合python作自己的机器人(下)
前言
承接上文,我们来继续开发QQ机器人
程序开发
消息事件
接口
上面我们实现了自我学习的机器人系统,这样就确保了日常对话已经不是问题。
下面我们来实现一些特殊功能,例如一些推送效果等
这里我们需要用到一些互联网上的API,我个人用的是这个网页的
在special_function目录中新建了一个help_msg.py文件,来返回帮助信息
def private_chat_help():
content="[CQ:face,id=63][CQ:face,id=63][CQ:face,id=63]%20专有命令:%0a"
content+="1-调教:%27#学习%20[目标语]%20[自动回复语]%27%20%0a"
content+="2-翻译:%27翻译%20[待翻译内容]%27%20%0a"
content+="3-手机号码信息:%27号码信息%20[手机号码]%27%20%0a"
content+="4-壁纸:%27壁纸%7C高清壁纸%27%20%0a"
content+="5-头像推荐:%27头像%20[女%7C男%7C动漫]%27%20%0a"
return content
特殊字符要url转码
然后还是这个目录,新建一个web_api_use.py,承载对网上api的调用,例如我们这里先做翻译功能:
#可用接口文档:https://api.66mz8.com/docs-translation.html
#可用接口文档:https://alapi.cn/doc/show/32.html
import requests
import json
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'
}
#翻译接口
def translate_api(content):
# 带参数的get请求
resp=requests.get(url='https://api.66mz8.com/api/translation.php', params={'info': content},headers=headers)
resp_dict=json.loads(resp.text)
if resp_dict["code"]==200:
return resp_dict["fanyi"]
else:
return "我不会!"
这里我又新建了一个handle目录下的文件,叫command_handle.py,来处理特殊的指令,这样我们就将message_handle.py进行一下重构
#message_handle.py
#对于 消息事件 的处理
from socket_operate.client import send_msg
from handle.command_handle import command_study_handle,command_help_handle,command_translate_handle
from handle.msg_handle import get_raw_message,get_number,get_user_id,get_message_type
from special_function.logging_tool import logging_put
from special_function.study import get_reply,random
#---------------------------------------------------------------
#私聊信息的普通检测
def private_msg_