Skip to content

alicebot.adapter.telegram

Telegram 协议适配器。

本适配器适配了 Telegram Bot API 协议。 协议详情请参考:Telegram Bot API

class TelegramAdapter

Bases: alicebot.adapter.Adapter, alicebot.adapter.telegram.api.TelegramAPI

Telegram 协议适配器。

  • Attributes

    • session (aiohttp.client.ClientSession)

    • app (Optional[aiohttp.web_app.Application])

    • runner (Optional[aiohttp.web_runner.AppRunner])

    • site (Optional[aiohttp.web_runner.TCPSite])

class Config

Bases: alicebot.config.ConfigModel

Telegram 适配器配置

  • Attributes

    • adapter_type (Literal['polling', 'webhook']) - 适配器运行模式

    • bot_token (str) - 从 BotFather 获取的 token 值。 参考:https://core.telegram.org/bots#how-do-i-create-a-bot

    • api_server (str) - 自定义 API 服务器

    • webhook_host (Optional[str]) - 自定义 Webhook 服务器地址

    • webhook_port (Optional[int]) - 自定义 Webhook 服务器端口

    • webhook_url (Optional[str]) - 自定义 Webhook 服务器路径

    • proxy (Optional[str]) - 代理服务器地址,为空时表示不使用代理

    • api_timeout (int) - 进行 API 调用时等待返回响应的超时时间。

async method call_api(self, api, *, response_type = None, **params)

调用 Telegram Bot API,协程会等待直到获得 API 响应。

  • Arguments

    • api (str) - API 名称。

    • response_type (Optional[type[~_T]]) - API 响应类型。

    • **params (Any) - API 参数。

  • Returns

    Type: Optional[~_T]

    API 响应。

  • Raises

    • NetworkError - 网络错误。

    • ActionFailed - API 请求响应 failed,API 操作失败。

async method handle_response(self, request)

处理响应。

  • Arguments

    • request (aiohttp.web_request.Request)
  • Returns

    Type: aiohttp.web_response.StreamResponse

async method handle_telegram_event(self, update)

处理 Telegram 事件。

  • Arguments

    • update (alicebot.adapter.telegram.model.Update) - 接收到的信息。
  • Returns

    Type: None

async method on_tick(self)

当轮询发生。

  • Returns

    Type: None

async method run(self)

适配器运行方法,适配器开发者必须实现该方法。

适配器运行过程中保持保持运行,当此方法结束后,AliceBot 不会自动重新启动适配器。

  • Returns

    Type: None

async method send(self, message, chat_id, disable_notification = None, protect_content = None, message_effect_id = None, reply_parameters = None, reply_markup = None, **kwargs)

Call seedMessage etc. APIs to send message.

  • Arguments

    • message (Union[str, alicebot.adapter.telegram.message.TelegramMessage, alicebot.adapter.telegram.media.TelegramMedia]) - The message, can be str, TelegramMessage or TelegramMedia.

    • chat_id (Union[int, str]) - Unique identifier for the target chat or username of the target channel (in the format @channelusername).

    • disable_notification (Optional[bool]) - Sends the message silently. Users will receive a notification with no sound.

    • protect_content (Optional[bool]) - Protects the contents of the sent message from forwarding and saving.

    • message_effect_id (Optional[str]) - Unique identifier of the message effect to be added to the message; for private chats only.

    • reply_parameters (Optional[alicebot.adapter.telegram.model.ReplyParameters]) - Description of the message to reply to.

    • reply_markup (Union[alicebot.adapter.telegram.model.InlineKeyboardMarkup, alicebot.adapter.telegram.model.ReplyKeyboardMarkup, alicebot.adapter.telegram.model.ReplyKeyboardRemove, alicebot.adapter.telegram.model.ForceReply, NoneType]) - Additional interface options. A JSON-serialized object for an inline keyboard,custom reply keyboard, instructions to remove a reply keyboard or to force a reply from the user.

    • **kwargs (Any) - Additional API parameters.

  • Returns

    Type: Any

    API Response.

  • Raises

    • ... - See call_api().

async method shutdown(self)

在适配器结束运行时运行的方法,用于安全地关闭适配器。

AliceBot 在接收到系统的结束信号后先发送 cancel 请求给 run 任务。 在所有适配器都停止运行后,会依次运行并等待所有适配器的 shutdown() 方法。 当强制退出时此方法可能未被执行。

  • Returns

    Type: None

async method startup(self)

在适配器开始运行前运行的方法,用于初始化适配器。

AliceBot 依次运行并等待所有适配器的 startup() 方法,待运行完毕后再创建 run() 任务。

  • Returns

    Type: None

Released under the MIT License.