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

Lan
Lan
2020-08-07 / 15 评论 / 2,877 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2020年08月07日,已超过1350天没有更新,若内容或图片失效,请留言反馈。

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

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

image.png

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

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

image.png

然后就是代码:

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

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

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

import json

import requests
from .configInfo import *

"""
By : Lan
Website : 
""" 

# 群组消息接受类
class GroupMess:
    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']  # 消息时间


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


# 发送消息类
class SendMessage:
    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": ""
        }

    '''
    发送图片消息需要有:toUser sendToType sendMsgType picUrl
    '''

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

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

# coding=utf-8
import logging
import time
import socketio
from botConfig.botClass import *
from botConfig.configInfo import *

sio = socketio.Client()


"""
By : Lan
Website : https://gitlab.com/Vastsa/lanpicbed/-/raw/master/
""" 

def beat():
    while True:
        sio.emit('GetWebConn', robotQQ)
        time.sleep(60)


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


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


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


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


def main():
    try:
        sio.connect(webapi, transports=['websocket'])
        sio.wait()
    except BaseException as e:
        logging.info(e)
        print(e)


if __name__ == '__main__':
    main()

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

0

评论 (15)

取消
  1. 头像
    沙盒梦想 作者
    Windows 10 · Google Chrome

    emmm这个该怎么添加消息处理和消息发送啊

    回复
  2. 头像
    沙盒梦想 作者
    Windows 10 · Google Chrome

    (指监听

    回复
  3. 头像
    Lan 作者
    Android Oreo · Google Chrome
    @ 访客w

    代码里面有监听事件消息。在那里面进行处理。发送消息用类sendmessge(在botclass.py)里面

    回复
  4. 头像
    访客 作者
    Windows 10 · Google Chrome

    好家伙,作者换了个名就是为了不被百度收录,你倒好,又给整收录了

    回复
  5. 头像
    访客 作者
    Windows 10 · Google Chrome

    接口地址是什么呀

    回复
  6. 头像
    Lan 作者
    Android Oreo · Google Chrome
    @ 久别人潮

    这个需要你自己安装一下opq你就知道了

    回复
    1. 头像
      访客 作者
      Android · Google Chrome
      @ Lan

      你没有设置服务器ip

      回复
  7. 头像
    访客A 作者
    Windows 10 · Google Chrome

    请问OPQ在哪儿安装和下载啊?百度都没搜到啥合适的。麻烦能给个链接吗?

    回复
  8. 头像
    沙盒梦想 作者
    Windows 10 · Google Chrome
    @ 访客

    [root@localhost chajian]# ls
    botConfig main.py
    [root@localhost chajian]# cd botConfig/
    [root@localhost botConfig]# ls
    botClass.py configInfo.py pycache
    [root@localhost botConfig]#

    回复
  9. 头像
    Lan 作者
    Windows 10 · Google Chrome
    @ 访客

    你可以加我QQ78013994

    回复
  10. 头像
    访客黑色闪光 作者
    Linux · Google Chrome

    哈喽

    回复