Esp8266 chat bot not working properly

Hi! I’m not sure if this is even the place to post this so if it isn’t just let me know!
I have a template for a relay connected to an Arduino thats controlled by chat. However as far as I understand the api has changed since this was written and no longer works as intended. I I dont know enough about twitch api to really know what needs to be changed to get it working so any help given would be greatly appreciated.
Heres the code im using:

 /*******************************************************************
    Connect to Twtich Chat with a Bot
   Created with code from TheOtherLoneStar (https://www.twitch.tv/theotherlonestar)
   Hackaday IO: https://hackaday.io/otherlonestar
   By Brian Lough (https://www.twitch.tv/brianlough)
   YouTube: https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA
Created with code from noycebru www.twitch.tv/noycebru
 *******************************************************************/
 
#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <IRCClient.h>

//define your default values here, if there are different values in config.json, they are overwritten.
#define secret_ssid "my ssid" 
#define IRC_SERVER   "irc.chat.twitch.tv"
#define IRC_PORT     6667
 
//------- Replace the following! ------
char ssid[] = "Wifi_name";       // your network SSID (name)
char password[] = "Wifi_pass";  // your network key
 
//The name of the channel that you want the bot to join
const String twitchChannelName = "Channel_name"; //this is case sensitive!
 
//The name that you want the bot to have
#define TWITCH_BOT_NAME "bot_name"
 
//OAuth Key for your twitch bot
// https://twitchapps.com/tmi/
#define TWITCH_OAUTH_TOKEN "oauth:"
 
 
//------------------------------
 
 
int relay = 5; //led lights this turns the digital relay on and off. this is the pin the digital relay is connected to
String ircChannel = "";
 
WiFiClient WiFiClient;
IRCClient client(IRC_SERVER, IRC_PORT, WiFiClient);
 
// put your setup code here, to run once:
void setup() {
  
  pinMode(5, OUTPUT);
  delay(2000);
  Serial.begin(9600);
  Serial.println();
 
  // Set WiFi to station mode and disconnect from an AP if it was Previously
  // connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);
 
  // Attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  IPAddress ip = WiFi.localIP();
  Serial.println(ip);
 
  ircChannel = "#" + twitchChannelName;
 
  client.setCallback(callback);
}
 
void loop() {
 
  // Try to connect to chat. If it loses connection try again
  if (!client.connected()) {
    Serial.println("Attempting to connect to " + ircChannel );
    // Attempt to connect
    // Second param is not needed by Twtich
    if (client.connect(TWITCH_BOT_NAME, "", TWITCH_OAUTH_TOKEN)) {
      client.sendRaw("JOIN " + ircChannel);
      Serial.println("connected and ready to rock");
      sendTwitchMessage("Ready to go Boss!");
    } else {
      Serial.println("failed... try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
    return;
  }
  client.loop();
}
 
void sendTwitchMessage(String message) {
  client.sendMessage(ircChannel, message);
}
 
 
void callback(IRCMessage ircMessage) {
  //Serial.println("In CallBack");
 
  if (ircMessage.command == "PRIVMSG" && ircMessage.text[0] != '\001') {
    //Serial.println("Passed private message.");
   
    ircMessage.nick.toUpperCase();
 
    String message("<" + ircMessage.nick + "> " + ircMessage.text);
 
    //prints chat to serial
    Serial.println(message);

//this is where you would replace these elements to match your streaming configureation. 
if (ircMessage.text.indexOf("subscribed") > -1 && ircMessage.nick == "STREAMELEMENTS")
      {
     
      digitalWrite(relay, HIGH);
      delay(10000);
      digitalWrite(relay, LOW);
      delay(25);
   
    }

    if (ircMessage.text.indexOf("streaming") > -1 && ircMessage.nick == "STREAMELEMENTS")
      {
     
      digitalWrite(relay, HIGH);
      delay(10000);
      digitalWrite(relay, LOW);
      delay(25);
   
    }

    if (ircMessage.text.indexOf("test") > -1 && ircMessage.nick == "channelname")
      {
       digitalWrite(relay, HIGH);
      delay(10000);
      digitalWrite(relay, LOW);
      delay(25);
      }
 
    return;
  }
}

the bot connects to the chat for a bit but it posts under the users name and doesnt do anything when the test response is typed in chat…

Now if I understand this correctly my issue is the ouath tokens and some of the endpoints have changed but i’m not sure what to change them to in order to allow the bot to read the chat.

For one, you’re not sending a PING every so often, so Twitch will disconnect you if it thinks the connection is stale.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.