From 850096ceaa67ffecce2b50c86897c9f5321d108b Mon Sep 17 00:00:00 2001 From: Akirami <66513481+A-kirami@users.noreply.github.com> Date: Wed, 7 Dec 2022 21:43:27 +0800 Subject: [PATCH] =?UTF-8?q?:memo:=20Docs:=20=E5=AE=8C=E5=96=84=20`?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E5=B9=B3=E5=8F=B0=20API`=20=E9=83=A8?= =?UTF-8?q?=E5=88=86=20(#1447)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: StarHeart --- website/docs/tutorial/call-api.md | 28 ++++++++++++++++--- .../version-2.0.0rc2/tutorial/call-api.md | 28 ++++++++++++++++--- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/website/docs/tutorial/call-api.md b/website/docs/tutorial/call-api.md index 1971ef51..9bf41294 100644 --- a/website/docs/tutorial/call-api.md +++ b/website/docs/tutorial/call-api.md @@ -14,16 +14,36 @@ options: NoneBot 提供了两种方式来调用机器人平台 API,两种方式都需要首先获得 Bot 实例,然后调用相应的方法。 -例如,如果需要调用机器人平台的 `get_user_info` API,可以这样做: +## 获取 Bot 实例 ```python from nonebot import get_bot -bot = get_bot("bot_id") +bot = get_bot() # 获取第一个已连接的 bot 实例 +bot = get_bot("bot_id") # 获取指定 bot_id 的 bot 实例 +``` + +在事件处理依赖中,我们可以使用更为简便的办法来获取 bot 实例,详情可以参考 [获取上下文信息-Bot](https://v2.nonebot.dev/docs/tutorial/plugin/create-handler#bot) + +```python +from nonebot.adapters import Bot + +async def handle_func(bot: Bot): # 通过依赖注入获取 bot 实例 + ... +``` + +## 调用 API + +如果需要调用某个机器人平台的 `get_user_info` API,我们可以使用以下任意一种方式: + +```python +# 通过 bot 实例上的魔术方法直接使用.操作符调用 API result = await bot.get_user_info(user_id=12345678) -await bot.call_api("get_user_info", user_id=12345678) + +# 通过 bot 实例上的 call_api 方法调用 API +result = await bot.call_api("get_user_info", user_id=12345678) ``` :::tip 提示 -API 由平台提供,请参考平台文档。 +实际可用的 API 由平台提供,请参考平台文档。 ::: diff --git a/website/versioned_docs/version-2.0.0rc2/tutorial/call-api.md b/website/versioned_docs/version-2.0.0rc2/tutorial/call-api.md index 1971ef51..9bf41294 100644 --- a/website/versioned_docs/version-2.0.0rc2/tutorial/call-api.md +++ b/website/versioned_docs/version-2.0.0rc2/tutorial/call-api.md @@ -14,16 +14,36 @@ options: NoneBot 提供了两种方式来调用机器人平台 API,两种方式都需要首先获得 Bot 实例,然后调用相应的方法。 -例如,如果需要调用机器人平台的 `get_user_info` API,可以这样做: +## 获取 Bot 实例 ```python from nonebot import get_bot -bot = get_bot("bot_id") +bot = get_bot() # 获取第一个已连接的 bot 实例 +bot = get_bot("bot_id") # 获取指定 bot_id 的 bot 实例 +``` + +在事件处理依赖中,我们可以使用更为简便的办法来获取 bot 实例,详情可以参考 [获取上下文信息-Bot](https://v2.nonebot.dev/docs/tutorial/plugin/create-handler#bot) + +```python +from nonebot.adapters import Bot + +async def handle_func(bot: Bot): # 通过依赖注入获取 bot 实例 + ... +``` + +## 调用 API + +如果需要调用某个机器人平台的 `get_user_info` API,我们可以使用以下任意一种方式: + +```python +# 通过 bot 实例上的魔术方法直接使用.操作符调用 API result = await bot.get_user_info(user_id=12345678) -await bot.call_api("get_user_info", user_id=12345678) + +# 通过 bot 实例上的 call_api 方法调用 API +result = await bot.call_api("get_user_info", user_id=12345678) ``` :::tip 提示 -API 由平台提供,请参考平台文档。 +实际可用的 API 由平台提供,请参考平台文档。 :::