Giter Site home page Giter Site logo

tmi-rs's Introduction

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(())
}

tmi-rs's People

Contributors

stkfd avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.