3

Hello i've tried to connect to the Twitch IRC Chat so i can try to make a simple chat bot for twitch but im struggling to make it work.

Error im getting: http://puu.sh/j3HwK/173a0388fb.png

and here is the code:

<?php

set_time_limit(0);
ini_set('display_errors', 'on');


function IRCBot()
{
    function IRC()
    {
        $config = array(
                'server'  => 'irc.twitch.tv', 
                'port'    => 6667, 
                'channel' => '#spiritboar',
                'name'    => 'bin4rybot', 
                'nick'    => 'Bin4ryBOT', 
                'pass'    => 'oauth:##########################' //http://twitchapps.com/tmi/
        );

        echo 'test';
        $server = array();
        $server['connect'] = fsockopen($config['server'], $config['port']);

        if($server['connect'])
        {
            echo 'test2';
            SendData("PASS " . $config['pass'] . "\n\r");
            SendData("NICK " . $config['nick'] . "\n\r");
            SendData("USER " . $config['nick'] . "\n\r");
            SendData("JOIN " . $config['channel'] . "\n\r");

            while(!feof($server['connect']))
            {
                echo 'test3';
            }
        }
    }

    function SendData($cmd)
    {
        global $server;
        fwrite($server['connect'], $cmd, strlen($cmd));
        echo "[SEND] $cmd <br>";
    }

    IRC();

}

IRCBot();

?>

So basicly i cant make it connect to the Twitch IRC, Please if someone can help me it would be much appreciated! :)

Bin4ry
  • 31
  • 1
  • 2

2 Answers2

2

I know it was asked soo manny years ago, but perhaps can help someone else today:

<?php    
set_time_limit(0);
ini_set('display_errors', 'on');
$server = array();    

function IRCBot()
{
    function IRC()
    {
        $server global;
        $config = array(
                'server'  => 'ssl://irc.chat.twitch.tv', 
                'port'    => 6697, 
                'channel' => '#twitch_channel',
                'nick'    => strtolower('twitch_username'), 
                'pass'    => 'oauth:twitch_oauth_token' //http://twitchapps.com/tmi/
        );

        $server['connect'] = @fsockopen($config['server'], $config['port']);

        if($server['connect'])
        {
            echo "[<] Starting connection with user: " . $config['nick'];
            SendData('CAP REQ :twitch.tv/tags');
            SendData('CAP REQ :twitch.tv/commands');
            SendData('CAP REQ :twitch.tv/membership');
            SendData("PASS " . $config['pass']);
            SendData("NICK " . $config['nick']);
            SendData("USER " . $config['nick'] . "  1 1 :" . $config['nick']);
            SendData("JOIN " . $config['channel']);

            while(!feof($server['connect']))
            {
               $server['READ_BUFFER'] = fgets($server['connect'], 1024);
               echo "[>] " . $server['READ_BUFFER'];
               flush();
            }
        }
    }

    function SendData($cmd)
    {
        global $server;
        @fwrite($server['connect'], $cmd . "\r\n");
        echo "[<] $cmd \r\n";
    }

    IRC();

}

IRCBot();

The most important thing to notice beyond the format of the commands send to twitch, it's that the SendData function does not send strlen on the fwrite function and the "\r\n" append to command string.

eladolo
  • 91
  • 6
  • Thanks for the script. I have put my credentials .. Everything is working except for chat messages list. `$data = fgets($server['connect'],256); echoo "[>] " . $data` This does not work for me. Can you tell me why ? – Mittul At TechnoBrave Jul 29 '20 at 06:33
  • you have a error in `echoo [>]`... its **echo**... and the length of the message its different (**256** instead of **1024**) _idk if that affect the answer from twitch but maybe your error could be there_ – eladolo Jul 31 '20 at 17:39
  • I had to change line 10 to `global $server;` to make it work. – oelna Apr 20 '21 at 20:13
0

In your IRC() function, you've declared $server, but not marked it as a global variable, thus it's not available in SendData(...).

SendData(...) then looks at the global version of $server, finds that it's not got an array element called connect, so returns null, which is where your error is coming from.

Add a global $server; before $server = array();, and try again.

As a second point, I think the line returns need to be \r\n instead of \n\r, but I'm not sure - it will depend on how strict the IRC server is.

stwalkerster
  • 1,646
  • 1
  • 20
  • 30
  • Thanks for the updates in script. I have put my credentials .. Everything is working except for chat messages list. `$data = fgets($server['connect'],256); echoo "[>] " . $data` This does not work for me. Can you tell me why ? – Mittul At TechnoBrave Jul 29 '20 at 06:33