Skip to content

parafoxia/chatto

Chatto

PyPi version Downloads GitHub Workflow Status Docs License

A unified API wrapper for YouTube and Twitch chat bots.

CPython versions 3.8 through 3.10 and PyPy version 3.8 are officially supported.

Windows, MacOS, and Linux are all supported.

Installation

To install the latest stable version of Chatto:

pip install chatto

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

To install the latest development version:

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

You may need to prefix these commands with a call to the Python interpreter depending on your OS and Python configuration.

Setup

Before you begin, you will need to have a Google Developers project with the YouTube Data API V3 enabled. You need an API key, and if you want to send and delete messages, you will need an OAuth client ID.

I made a video walking through all the necessary steps.

Creating a YouTube bot

To create a simple YouTube bot, you could do something like this:

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 relies on the /search endpoint to find a live broadcast from a channel, which is not 100% reliable. If you are having major issues getting Chatto to find your channel's live stream, you can pass the stream ID directly:

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

If you don't want to use OAuth, you can launch Chatto in read-only mode. Note that your bot will not be able to send or delete messages in this mode:

bot.run(read_only=True)

To learn how to make more advanced bots, check the documentation.

Creating a Twitch bot

Twitch bots are not yet supported.

Contributing

Contributions are very much welcome! To get started:

License

The Chatto module for Python is licensed under the BSD 3-Clause License.