视频教程:
windows布局go-cqhttp_哔哩哔哩_bilibili
链接中:send_private_msg #私聊接口
send_private_msg #群聊接口
user_id #好友QQ号
group_id #群号
CQ #发送文件类型
发信息
import requests
from flask import Flask, requestapp = Flask(__name__)@app.route('/', methods=["POST"])
def post_data():p='0'print(request.get_json())if request.get_json().get('message_type') == 'private': # 如果是私聊信息状态码# 获取需要的消息QQ_name = request.get_json().get('sender').get('nickname') # 发送者人的昵称叫啥QQ_id = request.get_json().get('sender').get('user_id') # 发送者的QQ号Xingxi_text = request.get_json().get('raw_message') # 发的什么东西# 给go-cqhttp的5700端口提交数据,类似于浏览器访问的形式requests.get("http://127.0.0.1:5900/send_private_msg?user_id={0}&message={1}".format(QQ_id, Xingxi_text)) #对方的QQ号,需要发送的信息return p # 对go-cqhttp进行相应,不然会出现三次重试app.run(debug=True, host='127.0.0.1', port=5909) #监听本机的5909端口(数据来源于go-cqhttp推送到5909端口的数据)
1.发信息给好友
requests.get("http://127.0.0.1:5900/send_private_msg?user_id={0}&message={1}".format(QQ_id, Xingxi_text)) #对方的QQ号,需要发送的信息
2.发信息到群聊
resp=requests.get("http://127.0.0.1:5900/send_group_msg?group_id={0}&message={1}".format(group_id, message)) #群号,信息
3.发文本转语音给好友
import requestsurl = f"http://127.0.0.1:5900/send_private_msg"
params = {"user_id":"2106359814","message":'[CQ:tts,text=你在干什么?]'
}
response = requests.post(url, json=params)
print(response.text)
4.发文本转语音给群聊
import requests
resp=requests.get(url='http://127.0.0.1:5900/send_group_msg?group_id=928918816&message=[CQ:tts,text="how are you"]')
print(resp.text)
5.发图片给好友
requests.get(url='http://127.0.0.1:5900/send_private_msg?user_id={0}&message={1}'.format(QQ_id, '[CQ:image,file=2.jpg]'))
6.发图片到群聊
resp=requests.get("http://127.0.0.1:5900/send_group_msg?group_id={0}&message={1}".format(group_id, '[CQ:image,file=2.jpg]'))
注意:图片需要放到go-cqhttp的目录下的data/images下面,否则无法发送,网络图片可以直接发送
7.发语音(发语音和视频需要安装FFmpeg)
import requests
#同一个音频文件这次发了,下次就不能再发了,发了也是不成功的,机器人要发另外一个音频或者信息才能继续发送
url = "http://localhost:5900/send_private_msg?user_id=2106359814&message=[CQ:record,file=http://m801.music.126.net/20230403205958/c993ff5654bb619890a3fc9c1faff208/jdymusic/obj/wo3DlMOGwrbDjj7DisKw/16672040292/1f33/8dcd/e4ab/fc3c42b47e36e7b5510921aa32d67a78.mp3]"resp = requests.get(url)
# 打印返回结果
print(resp.json())#群聊
import requests
resp=requests.get("http://127.0.0.1:5900/send_group_msg?group_id=928918816&message=[CQ:record,file=https://m801.music.126.net/20230403140032/8fb2605d55e17140cefd0ca471a9f373/jdymusic/obj/wo3DlMOGwrbDjj7DisKw/17718433824/acca/41eb/8112/efa4dce840121844afcb957bcb2d4fd1.mp3]")
print(resp.text)
8.发视频
import requests
#[CQ:video,file=http://baidu.com/1.mp4]
import requests
resp=requests.get("http://127.0.0.1:5900/send_private_msg?user_id=2106359814&message=[CQ:video,file=1.mp4]")
print(resp.text)#群聊
import requests
#[CQ:video,file=http://baidu.com/1.mp4]
import requests
resp=requests.get("http://127.0.0.1:5900/send_group_msg?group_id=928918816&message=[CQ:video,file=1.mp4]")
print(resp.text)
9.发表情
import requests
resp=requests.get(url='http://127.0.0.1:5900/send_private_msg?user_id={0}&message={1}'.format(2106359814,'[CQ:face,id=0]'))
print(resp.text)#群聊
import requests
resp=requests.get(url='http://127.0.0.1:5900/send_group_msg?group_id=928918816&message=[CQ:tts,text="what are you doing?"]')
print(resp.text)