Coder Social home page Coder Social logo

felis's Introduction

Felis

基于Actor模式的Python机器人框架

license python onebot

快速开始

  1. 为bot提供配置
    # neko.json
    {
        "name": "Neko",
        "driver": {
            "connect_url": "ws://url-to-server"
        }
    }
  2. 运行项目
    async def main():
        neko = Neko.from_file("neko.json")
        await neko.start()
    
    if __name__ == "__main__":
        asyncio.run(main())

使用命令

  1. 通过继承Command[T]MessageCommand新建自定义命令。新建的命令会自动通过Commands注册。
    @Commands.register(name="cmd", description="xxxx")
    class MyCommand(MessageCommand):
        @override
        async def handle_message(self, event: MessageEvent, message: Message) -> None:
            self.send_back(event, message)
  2. 配置client.commands
    # neko.json
    {
        ...
        client: {
            commands: ["cmd", ...]
        }
    }

扩展

Neko类提供了customized_setup作为扩展点,可以通过继承的方式进行扩展。

class MyEventHandler:
    @staticmethod
    def apply() -> Behavior[ClientMessage]:
        def on_message(_, message: ClientMessage) -> Behavior[ClientMessage]:
            print(message)
            return Behavior[ClientMessage].same

        def setup(context: ActorContext[ClientMessage]) -> Behavior[ClientMessage]:
            context.system.receptionist.tell(
                ReceptionistRequest.register(EVENT_KEY, context.self)
            )
            return Behaviors.receive_message(on_message)

        return Behaviors.setup(setup)

class Felis(Neko):
    @override
    def customized_setup(self, context: ActorContext[NekoMessage]) -> None:
        context.spawn(MyEventHandler.apply(), "event_handler")

在上例中,我们创建了一个自定义的Actor: MyEventHandler。它通过EVENT_KEY订阅了从服务器推送的事件信息并打印出来; 通过继承Neko类,我们得以在customized_setup中生成MyEventHandler

结构

graph RL;

round((Receptionist))-.EVENT_KEY.->Driver--data-->Adapter
Adapter--Events/Response-->round((Receptionist))
round((Receptionist))-.ACTION_KEY.->Client--request-->Adapter;
round((Receptionist))-.xx_KEY.->...
Loading

Felis结构主要分为DriverAdapterClient三部分:

  • Driver:用于与后端交互,包括接收事件和动作响应、发送动作请求。
  • Client:用于处理事件,如使用Command
  • AdapterDriverClient的中间件,用于转换并广播事件。用户自定义的Actor可以通过订阅EVENT_KEYACTION_KEY获取事件和动作信息。

felis's People

Stargazers

Yu Xiong avatar  avatar

Watchers

Tianqi Ren avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.