2020-07-05 20:39:34 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-07-11 17:32:03 +08:00
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
|
|
|
|
from nonebot.config import Config
|
|
|
|
|
2020-07-05 20:39:34 +08:00
|
|
|
|
|
|
|
class BaseBot(object):
|
|
|
|
|
2020-07-11 17:32:03 +08:00
|
|
|
def __init__(self, type: str, config: Config, *, websocket=None):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
async def handle_message(self, message: dict):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
async def call_api(self, api: str, data: dict):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class BaseMessageSegment(dict):
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
type_: Optional[str] = None,
|
|
|
|
data: Optional[Dict[str, str]] = None):
|
|
|
|
super().__init__()
|
2020-07-15 20:39:59 +08:00
|
|
|
if type_:
|
2020-07-11 17:32:03 +08:00
|
|
|
self.type = type_
|
|
|
|
self.data = data
|
|
|
|
else:
|
2020-07-15 20:39:59 +08:00
|
|
|
raise ValueError('The "type" field cannot be empty')
|
2020-07-11 17:32:03 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2020-07-15 20:39:59 +08:00
|
|
|
def __getitem__(self, item):
|
|
|
|
if item not in ("type", "data"):
|
|
|
|
raise KeyError(f'Key "{item}" is not allowed')
|
|
|
|
return super().__getitem__(item)
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
if key not in ("type", "data"):
|
|
|
|
raise KeyError(f'Key "{key}" is not allowed')
|
|
|
|
return super().__setitem__(key, value)
|
|
|
|
|
|
|
|
# TODO: __eq__ __add__
|
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self) -> str:
|
|
|
|
return self["type"]
|
|
|
|
|
|
|
|
@type.setter
|
|
|
|
def type(self, value: str):
|
|
|
|
self["type"] = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def data(self) -> Dict[str, str]:
|
|
|
|
return self["data"]
|
|
|
|
|
|
|
|
@data.setter
|
|
|
|
def data(self, data: Optional[Dict[str, str]]):
|
|
|
|
self["data"] = data or {}
|
|
|
|
|
2020-07-11 17:32:03 +08:00
|
|
|
|
|
|
|
class BaseMessage(list):
|
|
|
|
|
|
|
|
def __init__(self, message: str = None):
|
2020-07-05 20:39:34 +08:00
|
|
|
raise NotImplementedError
|
2020-07-11 17:32:03 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return ''.join((str(seg) for seg in self))
|