-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
895c28e
commit 187a0b4
Showing
19 changed files
with
295 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# QQ 群机器人 <span class="beta-tag">Beta</span> | ||
|
||
在 [QQ 开放平台](https://bot.q.qq.com/wiki/#_2-%E4%BC%81%E4%B8%9A%E4%B8%BB%E4%BD%93%E5%85%A5%E9%A9%BB) 完成企业主体入驻,即可创建可在 | ||
QQ 群聊里使用的 QQ 群机器人。 | ||
|
||
## 在公网下使用 | ||
|
||
QQ 群聊适配器需要在本地启动资源服务让腾讯端能够访问媒体资源,默认在公网下使用。 | ||
|
||
```python | ||
from amiyabot.adapters.tencent.qqGroup import qq_group | ||
|
||
client_secret = '******' # 密钥 | ||
|
||
bot = AmiyaBot(appid='******', token='******', adapter=qq_group(client_secret)) | ||
``` | ||
|
||
- 在机器人启动时,资源服务也会一同启动。 | ||
- 默认的资源服务是端口单例的,实例化多个 QQ 群聊适配器 AmiyaBot 或使用 [多账号](/develop/basic/multipleAccounts.html) | ||
时,同一个端口的资源服务会相互共享。 | ||
|
||
### 修改资源服务配置 | ||
|
||
引入 `QQGroupChainBuilderOptions` 修改默认的资源服务配置。 | ||
|
||
| 参数名 | 类型 | 释义 | 默认值 | | ||
|---------------------|------|-----------------------------------------------------------|------------| | ||
| host | str | 资源服务监听地址 | 0.0.0.0 | | ||
| port | int | 资源服务监听端口 | 8086 | | ||
| resource_path | str | 临时文件存放目录 | ./resource | | ||
| http_server_options | dict | [HttpServer **kwargs](/develop/advanced/httpSupport.html) | | | ||
|
||
```python | ||
from amiyabot.adapters.tencent.qqGroup import qq_group | ||
from amiyabot.adapters.tencent.qqGroup.builder import QQGroupChainBuilderOptions | ||
|
||
bot = AmiyaBot( | ||
appid='******', | ||
token='******', | ||
adapter=qq_group( | ||
client_secret='******', | ||
default_chain_builder_options=QQGroupChainBuilderOptions( | ||
'0.0.0.0', | ||
8086, | ||
'./resource', | ||
), | ||
), | ||
) | ||
``` | ||
|
||
## 自定义资源服务 | ||
|
||
多数情况下我们推荐使用第三方托管服务来搭建资源服务,如 [腾讯云COS](https://www.baidu.com/s?wd=%E8%85%BE%E8%AE%AF%E4%BA%91COS) | ||
或 [阿里云OSS](https://www.baidu.com/s?wd=%E9%98%BF%E9%87%8C%E4%BA%91OSS) 等。 | ||
|
||
通过自定义默认的 `ChainBuilder`,来实现上传文件到托管服务以及返回生成的 url。 | ||
|
||
普通 ChainBuilder 可参考 [进阶指南 - 介入媒体消息](/develop/advanced/chainBuilder.md) | ||
,但在这里更推荐继承适配器中的 `QQGroupChainBuilder`。 | ||
|
||
### 普通 ChainBuilder | ||
|
||
```python | ||
from typing import Union | ||
from graiax import silkcoder | ||
from amiyabot import ChainBuilder | ||
|
||
class MyBuilder(ChainBuilder): | ||
@classmethod | ||
async def get_image(cls, image: Union[str, bytes]) -> Union[str, bytes]: | ||
# 上传图片到第三方托管服务 | ||
... | ||
return url # 返回访问资源的 URL | ||
|
||
@classmethod | ||
async def get_voice(cls, voice_file: str) -> str: | ||
# 上传语音文件到第三方托管服务,语音文件必须是 silk 格式 | ||
voice: bytes = await silkcoder.async_encode(voice_file, ios_adaptive=True) | ||
... | ||
return url # 返回访问资源的 URL | ||
|
||
@classmethod | ||
async def get_video(cls, video_file: str) -> str: | ||
# 上传视频文件到第三方托管服务 | ||
... | ||
return url # 返回访问资源的 URL | ||
|
||
|
||
bot = AmiyaBot(..., adapter=qq_group(default_chain_builder=MyBuilder())) | ||
``` | ||
|
||
### 继承 QQGroupChainBuilder | ||
|
||
🚧 文档编写中... 🚧 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# 介入 Chain 构建媒体消息时的操作 | ||
|
||
Chain 对象在构建消息时,可使用辅助类介入媒体消息或浏览器的构建过程。 | ||
|
||
## 创建辅助类 | ||
|
||
继承 `ChainBuilder` 类并覆写其方法创建一个自定义的辅助类。在实例化 Chain 时,传入辅助类实例即可介入 Chain 的操作。 | ||
|
||
```python | ||
from typing import Union | ||
from amiyabot import Chain, ChainBuilder | ||
from playwright.async_api import Page | ||
|
||
class MyBuilder(ChainBuilder): | ||
@classmethod | ||
async def get_image(cls, image: Union[str, bytes]) -> Union[str, bytes]: | ||
... | ||
return image | ||
|
||
@classmethod | ||
async def get_voice(cls, voice_file: str) -> str: | ||
... | ||
return voice_file | ||
|
||
@classmethod | ||
async def get_video(cls, video_file: str) -> str: | ||
... | ||
return video_file | ||
|
||
@classmethod | ||
async def on_page_rendered(cls, page: Page): | ||
... | ||
|
||
|
||
# 在构造参数里使用辅助类 | ||
chain = Chain(..., chain_builder=MyBuilder()) | ||
|
||
# 为属性赋值使用辅助类 | ||
chain.builder = MyBuilder() | ||
``` | ||
|
||
## get_image | ||
|
||
该函数会在 chain 构建图片消息时调用,每张图片调用一次。传入一个参数 `image` ,类型为 `str`(文件路径或 url) 或 | ||
`bytes`(图片字节数据)。<br> | ||
如果函数有返回值(必须是以上两种类型),chain 会使用返回值构建图片消息。 | ||
|
||
## get_voice | ||
|
||
该函数会在 chain 构建语音消息时调用,每个语音文件调用一次。传入文件路径 `voice_file`。<br> | ||
如果函数有返回值,chain 会使用返回值构建语音消息。 | ||
|
||
## get_video | ||
|
||
该函数会在 chain 构建视频消息时调用,每个视频文件调用一次。传入文件路径 `video_file`。<br> | ||
如果函数有返回值,chain 会使用返回值构建视频消息。 | ||
|
||
## on_page_rendered | ||
|
||
该函数会在 chain 构建浏览器渲染的图片并打开了页面时调用,提供了浏览器的 `Page` 对象,可在此对 `Page` 进行操作(如对页面进行 | ||
JS 注入等)。 | ||
|
||
::: tip 提示 | ||
构建浏览器渲染的图片同样也会调用一次 `get_image` 函数。 | ||
::: | ||
|
||
**使用示例** | ||
|
||
```python | ||
class BaiduSearch(ChainBuilder): | ||
@classmethod | ||
async def on_page_rendered(cls, page: Page): | ||
""" | ||
可以在截图前先对页面进行操作,比如 ”百度一下“ | ||
""" | ||
await page.locator('#kw').fill('AmiyaBot') | ||
await page.locator('#su').click() | ||
await asyncio.sleep(2) | ||
|
||
|
||
@bot.on_message(keywords='hello') | ||
async def _(data: Message): | ||
chain = Chain(data, chain_builder=BaiduSearch()) | ||
|
||
return chain.html( | ||
'https://www.baidu.com/', | ||
is_template=False, | ||
render_time=1000, | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.