Skip to content
/ tmi-rs Public

An asynchronous Rust interface to Twitch chat

Notifications You must be signed in to change notification settings

stkfd/tmi-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tmi-rs: Rust Twitch chat interface

Note: there is currently a larger rework of the internal connection handling pending, progress can be followed in PR #9

This is an asynchronous websocket based interface to Twitch chat intended as a base for chat bots and other programs that interact with Twitch chat.

All official chat events and tags are supported, but some more advanced features are still missing, including:

  • Ability to directly await the results of commands that have a response from Twitch, like /mods

Example usage

#[macro_use]
extern crate log;

use std::env;
use std::error::Error;
use std::sync::Arc;

use tokio::stream::StreamExt;

use pin_utils::pin_mut;
use tmi_rs::client_messages::ClientMessage;
use tmi_rs::event::*;
use tmi_rs::selectors::priv_msg;
use tmi_rs::{single::connect, TwitchClientConfig, TwitchClientConfigBuilder};

/// To run this example, the TWITCH_CHANNEL, TWITCH_USERNAME and TWITCH_AUTH environment variables
/// need to be set.
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    env_logger::init();
    let channel = env::var("TWITCH_CHANNEL")?;
    let config: Arc<TwitchClientConfig> = Arc::new(
        TwitchClientConfigBuilder::default()
            .username(env::var("TWITCH_USERNAME")?)
            .token(env::var("TWITCH_AUTH")?)
            .build()?,
    );
    let mut client = connect(&config).await?;
    let mut sender = client.sender_cloned();
    let receiver = client.stream_mut();

    // join a channel
    sender.send(ClientMessage::join(channel.clone())).await?;
    info!("Joined channel");

    // process messages and do stuff with the data
    let privmsg_stream = receiver
        .filter_map(|event| match event {
            Ok(event) => Some(event),
            Err(error) => {
                error!("Connection error: {}", error);
                None
            }
        })
        .filter_map(priv_msg); // filter only privmsg events

    pin_mut!(privmsg_stream);

    while let Some(event_data) = privmsg_stream.next().await {
        info!("{:?}", event_data);
        if event_data.message().starts_with("!hello") {
            // return response message to the stream
            sender
                .send(ClientMessage::message(
                    event_data.channel().to_owned(),
                    "Hello World!",
                ))
                .await?;
        }
    }
    Ok(())
}

About

An asynchronous Rust interface to Twitch chat

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages