調(diào)用接口
一、 概述
1、 簡介
在我們實現(xiàn)了權(quán)限控制功能后,那么我們就在也不用擔(dān)心機器人話太多,同時,平時又沒時間,電腦又不在身邊,而無法控制機器人了。那么,我們實現(xiàn)了權(quán)限的管理就好說了。然后,又出現(xiàn)一個問題了,我們應(yīng)該如何利用這個權(quán)限系統(tǒng),來幫助我們管理機器人呢?
這個大家馬上就會知曉了,我會從如何控制智能聊天,以及控制戳一戳,再接入一些功能接口。來講述,如何使用這個權(quán)限功能。
2、 接口
其次,我們還需要了解一下,什么是接口呢?
應(yīng)用程序接口(API,Application Programming Interface)是基于編程語言構(gòu)建的結(jié)構(gòu),使開發(fā)人員更容易地創(chuàng)建復(fù)雜的功能。它們抽象了復(fù)雜的代碼,并提供一些簡單的接口規(guī)則直接使用。
舉一個例子:想想您的房子、公寓或其他住宅的供電方式,如果您想在您的房子里用電,只要把電器的插頭插入插座就可以,而不是直接把它連接到電線上——這樣做非常低效,而且對于不是電工的人會是困難和危險的。
同時,可以根據(jù)這張圖來理解API的功能,其就是可以給客戶端來提供數(shù)據(jù)的。
我們訪問一些特定的API就可以得到我們想要的數(shù)據(jù),這有一些較為好用的API提供方:
下面,我將來告訴大家,API的使用方法
二、 接入權(quán)限系統(tǒng)
在調(diào)用API之前,我們先來學(xué)習(xí)一下,如何給戳一戳和智能聊天接入我們的權(quán)限管理系統(tǒng),私聊的權(quán)限這里就不做展示了。大家可以根據(jù)這幾次的文章,開發(fā)一個私聊的權(quán)限。
1、 智能聊天
我是這樣設(shè)置的,為了防止機器人回的消息過多,只有艾特機器人的消息,它才會回,當(dāng)然這是基于權(quán)限的基礎(chǔ)上了,那么,如何接入呢?
請大家看代碼:
if _.get("message_type") == "group" and "[CQ:at,qq=2786631176]" in _["raw_message"]: # 即使用in來判斷其是否為艾特機器人的消息
# 制作群聊消息
db = current_app.config["db"]
session = db.session
group = session.query(Group).filter(db.and_(Group.qqId == _["group_id"], Group.isDetect)).first() # 同時這個群要支持機器人聊天,才會進入下一步
if group and group.group2auth.chat: # 如果允許聊天的話,就在群內(nèi)開啟聊天功能,這個是基于if的權(quán)限管理系統(tǒng)
asyncio.run(GroupChatMes.handle_group(_))
# GroupChatMes.handle_group的內(nèi)容
async def handle_group(resp):
message = resp["raw_message"].replace("[CQ:at,qq=2786631176]", "") # 獲取發(fā)送過來的消息
gid = resp["group_id"] # 獲取發(fā)送消息的群號
# 處理群聊信息
if message.strip() == "":
await send(gid, "艾特我干啥?又不發(fā)消息,一巴掌呼死你![CQ:face,id=86][CQ:face,id=12]", "group")
else:
# 調(diào)用接口,來獲取回復(fù),同時這里使用了異步請求
async with httpx.AsyncClient() as client:
params = {
"key": "free",
"appid": 0,
"msg": message,
}
resp = await client.get("http://api.qingyunke.com/api.php", params=params)
_ = resp.json()
ret = _.get("content", "獲取回復(fù)失敗")
await send(gid, ret, "group") # 發(fā)送群組消息
2、 戳一戳
戳一戳,就回復(fù),這個功能是有點煩人的,如果是討厭這個機器人的話,那當(dāng)然就需要進行權(quán)限的控制啦!廢話不多說,直接上代碼
if _.get("sub_type") == "poke":
# 如果事件類型為戳一戳
asyncio.run(GroupAndPri.click_event(_))
# GroupAndPri.click_event的內(nèi)容
async def click_event(resp):
uid = resp["user_id"]
tid = resp["target_id"]
if str(tid) != "3500515050" and str(tid) != "2786631176": # 如果不是這兩個賬號的戳一戳,則不管
return
try:
gid = resp["group_id"]
db = current_app.config["db"]
session = db.session
group = session.query(Group).filter(db.and_(Group.qqId == gid, Group.isDetect)).first() # 同時這個需要被檢測到,如果為群戳戳的話
except KeyError as e:
gid = None
group = None
# 處理戳一戳的消息
info = choice(current_app.config.get("CLICK_MES")) # 獲取戳一戳需要回復(fù)的的信息
try:
info = info % uid
except Exception as e:
if gid: # 說明其為群戳戳
info = f"[CQ:at,qq={uid}]" + info
if gid is None: # 其為私聊信息
await send(uid, info, "private") # 發(fā)送信息,這個函數(shù)在上一篇文章中實現(xiàn)過
elif gid and group.group2auth.click: # 如果允許發(fā)送戳一戳的內(nèi)容的話,就發(fā)送,反之,什么都不做,進行權(quán)限的判斷
await send(gid, info, "group")
3、 新成員
對于新成員的歡迎也是如此,也需要進行權(quán)限的管理
if _.get("notice_type") == "group_increase":
# 有新成員加入
db = current_app.config["db"]
session = db.session
group = session.query(Group).filter(db.and_(Group.qqId == _["group_id"], Group.isDetect)).first()
if group and group.group2auth.welcome: # 開啟歡迎的功能的話,就繼續(xù),否則返回
asyncio.run(GroupChatMes.group_increase(_)) # 發(fā)送歡迎語
# GroupChatMes.group_increase的內(nèi)容
async def group_increase(resp):
uid = resp["user_id"] # 獲取加入者的qq
gid = resp["group_id"] # 獲取群號
# 處理有新成員加入的情況
welcome_group = current_app.config.get("WELCOME_MES") # 從配置文件中獲取歡迎的語句
msg = welcome_group.get(str(gid),
welcome_group["default"]) % uid # welcome_group的鍵是qq群號,值是歡迎語
await send(gid, msg, "group") # 發(fā)送信息
三、 調(diào)用接口
1、 查找接口
上面說了那么多,還沒說到今天的重點,今天的重點就是實現(xiàn)機器人的主要功能,給群聊增加一些小功能,這些小功能的實現(xiàn),是通過調(diào)用接口,或者自己編程實現(xiàn)的?那么,對比這兩者,你更傾向于哪一種呢?我想是調(diào)用被人封裝好的接口吧,簡單又方便,相對來說也較為穩(wěn)定。
這里推薦一些提供質(zhì)量較好的接口的網(wǎng)站吧!
- https://www.yuanxiapi.cn/
- http://www.alapi.cn/api/list/
- https://api.vvhan.com/
- http://bjb.yunwj.top/php/API/html.html
- …
如果還有其他較好用的接口,可以在評論區(qū)下方留言一起分享哦!
好,接口在哪找我們知道了!那怎么用呢?
2、 調(diào)用接口
其實調(diào)用方法非常簡單,就是使用網(wǎng)絡(luò)請求工具,對這個地址發(fā)送請求,我們就可以得到這個接口的內(nèi)容了!
# 比如,我對http://bjb.yunwj.top/php/tp/lj.php這個接口發(fā)送請求
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
__author__ = "A.L.Kun"
__file__ = "handle.py"
__time__ = "2022/9/9 19:53"
import httpx
import re
resp = httpx.get("http://bjb.yunwj.top/php/tp/lj.php")
data = re.findall('"tp":"(?P<url>.*?)"', resp.text)[0]
print(data)
3、 接入機器人
在知道如何調(diào)用這個接口后,我們就可以嘗試的把這個接入我們的機器人中去了!
首先,我們需要做一個消息分發(fā)的功能
# 群聊小心分發(fā)的方式為:
if _.get("message_type") == "group":
# 獲取群命令
db = current_app.config["db"]
session = db.session
group = session.query(Group).filter(db.and_(Group.qqId == _["group_id"], Group.isDetect)).first()
if not group:
return
message = _.get("raw_message")
if message.startswith("/"): # 如果這個函數(shù)是以/開頭的話,說明其為命令,可能為管理員命令,也可能是普通命令,進行進一步分判別
asyncio.run(Command.command(_, "group", group.group2auth.smallFunction)) # 同時傳入這個群是否開啟了拓展功能,方便后面的判斷
async def command(resp, ty, isornot=None): # 對命令進行分類,如果這個消息是/開頭的話,就直接進入到這個函數(shù)中,同時/開頭的消息,還有管理員信息,故需要進行一個群區(qū)分
"""
對所有命令進行處理
:param resp: post的數(shù)據(jù)
:param ty: 消息來自哪里
:param isornot: 當(dāng)為群消息時,要傳入是否支持拓展功能
:return:
"""
uid = resp["sender"]["user_id"] # 獲取發(fā)信息的好友qq號
message = resp["raw_message"] # 獲取發(fā)送過來的消息
if ty == "group":
gid = resp["group_id"] # 獲取群號
else:
gid = None
if message.startswith("/admin:"):
"""管理系統(tǒng)的命令"""
if str(uid) in current_app.config["SUPER_USER"] and ty == "private":
await Command.super_command(uid, message)
elif ty == "group" and resp["sender"]["role"] in ["owner", "admin"]: # 判斷運行這個命令的是否為群管理員
# 說明這個是群管理員的修改
await Command.admin_command(uid, gid, message)
else:
await Sender.send(uid if ty == "private" else gid, "權(quán)限不足!", ty)
else:
if isornot or ty == "private": # 查看是否開啟這個功能,是私聊消息,或者開啟了拓展功能
"""管理拓展功能的命令"""
await Command.com_command(uid if gid is None else gid, message, ty, resp)
else:
await Sender.send(resp["group_id"], "本群沒有開啟拓展功能哦!請聯(lián)系管理員開啟這個功能!", "group")
進行分發(fā)完后,我們就需要對命令進行解析了:文章來源:http://www.zghlxwxcb.cn/news/detail-624270.html
async def com_command(id, message, ty, resp): # 處理一般的命令
"""
id : uid / gid
messge : 命令消息
ty: 這個消息是從哪里來的
resp: 同時把響應(yīng)的結(jié)果也傳入
"""
uid = resp["sender"]["user_id"]
base = "[CQ:at,qq=" + str(uid) + "]" if ty == 'group' else ""
command = re.findall(r"/(.*)", message.split()[0])[0] # 先獲取一個類似函數(shù)的命令,作為啟動
if command == "bing":
msg = await ConnectApi.get_bing()
await Sender.send(id, msg, ty)
elif command == "天氣":
if len(message.split()) != 2:
await Sender.send(id, "輸入格式錯誤,請根據(jù)幫助文檔輸入!", ty)
return
city = message.split()[1]
msg = await ConnectApi.weather(city)
await Sender.send(id, msg, ty)
elif command == "send": # 提建議給開發(fā)者
if len(message.split()) < 2:
await Sender.send(id, "輸入格式錯誤,請根據(jù)幫助文檔輸入!", ty)
return
content = "\n".join(message.split()[1:]) + f"\n——{uid}"
await Sender.send(current_app.config["ADMIN"], content, "private")
await Sender.send(id, base + "收到,謝謝您的建議![CQ:face,id=63][CQ:face,id=63][CQ:face,id=63]", ty)
elif command == "簡報": # 獲取每日簡報
msg = await ConnectApi.brief()
await Sender.send(id, msg, ty)
# 實現(xiàn)的函數(shù)的代碼
class ConnectApi:
"""連接接口,創(chuàng)建拓展功能"""
@staticmethod
async def get_bing():
"""
獲取bing的每日一圖
:return:
"""
return "[CQ:image,file=https://www.yuanxiapi.cn/api/bing/,cache=0]" # 這個網(wǎng)站就是一個接口
@staticmethod
async def weather(city):
"""天氣數(shù)據(jù)獲取"""
async with httpx.AsyncClient(headers={
"user-agent": UserAgent().random,
}) as client:
resp = await client.get("https://api.wpbom.com/api/weather.php?city=%s" % city) # 向接口中傳入城市信息
data = resp.json() # 獲取返回的JSON數(shù)據(jù)
try:
if data["status"] == 1000:
# 獲取成功,對數(shù)據(jù)進行解析
city = data["data"]["city"]
wea = data["data"]["forecast"][0]
high = re.findall(r".*?(?P<tem>\d.*)", wea["high"])[0]
low = re.findall(r".*?(?P<tem>\d.*)", wea["low"])[0]
type_ = wea["type"]
ganmao = data["data"]["ganmao"]
ret = f"{city}今日天氣{type_}:\n{low}~{high}\n溫馨提示:{ganmao}"
else:
raise ValueError
except Exception as e:
# 接口獲取失敗
ret = f"{city}天氣數(shù)據(jù)獲取失??!"
return ret
@staticmethod
async def brief():
# 獲取每日簡報
async with httpx.AsyncClient() as client:
resp = await client.get("http://bjb.yunwj.top/php/tp/lj.php") # 同時,對于異步任務(wù),記得要掛起
url = re.findall('"tp":"(?P<url>.*?)"', resp.text)[0]
return f"[CQ:image,file={url},cache=0]" # 使用cq碼發(fā)送我們的圖片信息
好了,今天的文章就到這里結(jié)束了,不知道您學(xué)到了多少呢?代碼,在我完成全部的功能之后,會在GitHub開源出來,所以大家不用擔(dān)心代碼的問題?,F(xiàn)在只需要理解這些功能的實現(xiàn)原理。當(dāng)然,能根據(jù)自己的理解,寫一個屬于自己的機器人就更好了!文章來源地址http://www.zghlxwxcb.cn/news/detail-624270.html
到了這里,關(guān)于go-cqhttp調(diào)用接口的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!