示例#1
0
 def __init__(self, qq: int, port: int = None, host: str = None, timeout=20):
     self.qq = qq
     self.config = Config(host=host, port=port)
     self.c = httpx.AsyncClient(
         headers={'Content-Type': 'application/json'},
         timeout=timeout + 5,
         base_url=self.config.address,
         params={'qq': self.qq, 'timeout': timeout},
     )
示例#2
0
    def __init__(
        self,
        *,
        qq: Union[int, List[int]] = None,
        use_plugins: bool = True,
        port: int = None,
        host: str = None,
        group_blacklist: List[int] = None,
        friend_blacklist: List[int] = None,
        blocked_users: List[int] = None,
        log: bool = True,
        log_file: bool = True,
    ):
        if qq is not None:
            if isinstance(qq, Sequence):
                self.qq = list(qq)
            else:
                self.qq = [qq]
        else:
            self.qq = None

        self.use_plugins = use_plugins
        self.config = Config(
            host, port, group_blacklist, friend_blacklist, blocked_users
        )

        # 作为程序是否应该退出的标志,以便后续用到
        self._exit = False

        if log:
            if log_file:
                enble_log_file()
        else:
            logger.disable(__name__)

        # 消息接收函数列表
        # 这里只储存主体文件中通过装饰器或函数添加的接收函数
        self._friend_msg_receivers = []
        self._group_msg_receivers = []
        self._event_receivers = []

        # 消息上下文对象中间件列表
        # 中间件以对应消息上下文为唯一参数,返回值与上下文类型一致则向下传递
        # 否则直接丢弃该次消息
        self._friend_context_middlewares = []
        self._group_context_middlewares = []
        self._event_context_middlewares = []

        # webhook
        if self.config.webhook:
            from . import webhook  # pylint: disable=C0415

            self._friend_msg_receivers.append(webhook.receive_friend_msg)
            self._group_msg_receivers.append(webhook.receive_group_msg)
            self._event_receivers.append(webhook.receive_events)

        # 插件管理
        # 管理插件提供的接收函数
        self.plugMgr = PluginManager()
        if use_plugins:
            self.plugMgr.load_plugins()
            print(self.plugin_status)

        # 当连接上或断开连接运行的函数
        # 如果通过装饰器注册了, 这两个字段设置成(func, every_time)
        # func 是需要执行的函数, every_time 表示是否每一次连接或断开都会执行
        self._when_connected_do: Tuple[Callable, bool] = None
        self._when_disconnected_do: Tuple[Callable, bool] = None

        # 线程池 TODO: 开放该参数
        thread_works = 50
        self.pool = WorkerPool(thread_works)

        # 依次各种初始化
        self._initialize_socketio()
        self._initialize_handlers()
示例#3
0
import httpx

from botoy import EventMsg, FriendMsg, GroupMsg
from botoy.config import Config
from botoy.log import logger

# 以内置插件方式实现简单的webhook,将原始数据上报至指定地址
# 因为webhook功能只能通过配置文件开启,所以直接新建Config, 读取配置文件即可
# TODO: resolve response

_config = Config()
_c = httpx.Client(base_url=_config.address, timeout=_config.webhook_timeout)


def receive_friend_msg(ctx: FriendMsg):
    try:
        _c.post(httpx.URL(), json=ctx.message)
    except Exception as e:
        logger.warning('Webhook请求中的错误: %s' % e)


def receive_group_msg(ctx: GroupMsg):
    try:
        _c.post(httpx.URL(), json=ctx.message)
    except Exception as e:
        logger.warning('Webhook请求中的错误: %s' % e)


def receive_events(ctx: EventMsg):
    try:
        _c.post(httpx.URL(), json=ctx.message)