当前位置:网站首页 > 更多 > 编程开发 > 正文

[Python] OPQ机器人框架插件开发之Python模板

作者:CC下载站 日期:2020-08-07 00:00:00 浏览:48 分类:编程开发

今天了解到还有个OPQ的框架似乎还不错,于是研究了一下,比酷Q还方便(因为我之前用的air版,图片都发不了)

并且支持多平台,但是最新的版本居然阉割了Windows下的许多功能

不过问题不大,满足我之前的需求了。

于是参考了一下原本iot的python模板然后进行了修改,更加符合自己的用法习惯吧。

然后就是代码:

botConfig->configInfo.py(用于存放接口地址,以及机器人QQ)

robotQQ="{机器人QQ}"
webapi="{接口地址,后面无需加/}"

botConfig ->botClass.py (用于存放一些对消息的解析类)

importjson

importrequests
from.configInfoimport*

"""
By:Lan
Website:
"""

#群组消息接受类
classGroupMess:
def__init__(self,message):
self.FromQQG=message['FromGroupId']#来源QQ群
self.QQGName=message['FromGroupName']#来源QQ群昵称
self.FromQQ=message['FromUserId']#来源QQ
self.FromQQName=message['FromNickName']#来源QQ名称
self.Content=message['Content']#消息内容
self.MsgTime=message['MsgTime']#消息时间


#私发消息接受类
classPrivateMess:
def__init__(self,message):
self.FromQQ=message['ToUin']#来源QQ号
self.ToQQ=message['FromUin']#目标QQ号
self.Content=message['Content']#消息内容
self.MsgType=message['MsgType']#消息类型


#发送消息类
classSendMessage:
def__init__(self,toUser=0,sendToType=0,sendMsgType='',groupId=0,content='',atUser=0,voiceUrl='',picUrl='',
picBase64Buf='',voiceBase64Buf=''):
self.data={
"toUser":toUser,#欲发给的对象群或QQ好友或私聊对象
"sendToType":sendToType,#发送消息对象的类型1好友2群3私聊
"sendMsgType":sendMsgType,#欲发送消息的类型TextMsg、PicMsg、VoiceMsg
"content":content,#发送的文本内容
"groupid":groupId,#发送私聊消息是在此传入群ID其他情况为0
"atUser":atUser,#At用户传入用户的QQ号其他情况为0
"picUrl":picUrl,#发送图片的网络地址
"picBase64Buf":picBase64Buf,#发本地送图片的buf转bas64编码
"voiceUrl":voiceUrl,#发送语音的网络地址
"voiceBase64Buf":voiceBase64Buf,#发本地送语音的buf转bas64编码
"fileMd5":""
}

'''
发送图片消息需要有:toUsersendToTypesendMsgTypepicUrl
'''

defsend(self):
print(self.data)
requests.post(url=f'{webapi}/v1/LuaApiCaller?qq={robotQQ}&funcname=SendMsg&timeout=10',
data=json.dumps(self.data))
return200

main.py (用于存放主文件)

#coding=utf-8
importlogging
importtime
importsocketio
frombotConfig.botClassimport*
frombotConfig.configInfoimport*

sio=socketio.Client()


"""
By:Lan
Website:https://pan.lanol.cn/
"""

defbeat():
whileTrue:
sio.emit('GetWebConn',robotQQ)
time.sleep(60)


@sio.event
defconnect():
print('connectedtoserver')
sio.emit('GetWebConn',robotQQ)#取得当前已经登录的QQ链接
beat()#心跳包,保持对服务器的连接


@sio.on('OnGroupMsgs')
defOnGroupMsgs(message):
"""监听群组消息"""
data=GroupMess(message['CurrentPacket']['Data'])
print(data)
return


@sio.on('OnFriendMsgs')
defOnFriendMsgs(message):
"""监听好友消息"""
data=PrivateMess(message['CurrentPacket']['Data'])
print(data)
return


@sio.on('OnEvents')
defOnEvents(message):
"""监听相关事件"""
print(message)


defmain():
try:
sio.connect(webapi,transports=['websocket'])
sio.wait()
exceptBaseExceptionase:
logging.info(e)
print(e)


if__name__=='__main__':
main()

GitHub地址:https://github.com/vastsa/OPQBot-Pyhon

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯