Skip to main content

YouTube 和 Twitch 聊天机器人的统一 API 包装器。

项目描述

和...聊天

派皮版本 下载 GitHub 工作流状态 文档 执照

YouTube 和 Twitch 聊天机器人的统一 API 包装器。

官方支持 CPython 3.8 至 3.10 版和 PyPy 3.8 版。

支持 Windows、MacOS 和 Linux。

安装

要安装Chatto的最新稳定版本:

pip install chatto

# If you need types:
pip install "chatto[types]"

要安装最新的开发版本:

pip install git+https://github.com/parafoxia/chatto

根据您的操作系统和 Python 配置,您可能需要调用 Python 解释器来为这些命令添加前缀。

设置

在开始之前,您需要有一个启用了 YouTube 数据 API V3 的 Google Developers 项目。您需要一个 API 密钥,如果您想发送和删除消息,您将需要一个 OAuth 客户端 ID。

制作了一个视频,介绍了所有必要的步骤。

创建 YouTube 机器人

要创建一个简单的 YouTube 机器人,您可以执行以下操作:

import os

from chatto import YouTubeBot
from chatto.events import MessageCreatedEvent

bot = YouTubeBot(
    # Your project's API key.
    os.environ["API_KEY"],
    # The ID of the channel whose stream you want to connect to.
    os.environ["CHANNEL_ID"],
    # Your OAuth client ID secrets file.
    secrets_file="secrets.json",
)


# Listen for MessageCreatedEvents, and run this awaitable whenever a
# new message is received.
@bot.listen(MessageCreatedEvent)
async def on_message_created(event):
    # Ignore messages sent by the broadcaster.
    if event.message.channel.is_owner:
        return

    # Respond to messages starting with "!hello".
    if event.message.content.startswith("!hello"):
        await bot.send_message(f"Hi {event.message.channel.name}!")


if __name__ == "__main__":
    # This is blocking, so should be the last thing you call.
    bot.run()

Chatto 依靠/search端点从频道中找到直播,这不是 100% 可靠的。如果您在让 Chatto 查找您频道的直播时遇到重大问题,您可以直接传递直播 ID:

bot.run(with_stream_id=os.environ["STREAM_ID"])

如果您不想使用 OAuth,可以以只读模式启动 Chatto。请注意,您的机器人将无法在此模式下发送或删除消息:

bot.run(read_only=True)

要了解如何制作更高级的机器人,请查看文档

创建 Twitch 机器人

尚不支持 Twitch 机器人。

贡献

非常欢迎贡献!开始:

执照

Python的Chatto模块在BSD 3-Clause License下获得许可。

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

chatto-0.5.0.tar.gz (18.8 kB 查看哈希)

已上传 source

内置分布

chatto-0.5.0-py3-none-any.whl (27.6 kB 查看哈希)

已上传 py3