Issue connecting to Twitch IRC for chatbot creation
I’m working on building a basic chatbot for Twitch and I need to connect to their IRC system. However, I keep running into problems when trying to establish the connection.
Here’s my current approach:
<?php
set_time_limit(0);
ini_set('display_errors', 'on');
function createTwitchBot()
{
function connectToIRC()
{
$settings = array(
'host' => 'irc.twitch.tv',
'port' => 6667,
'room' => '#streamername',
'username' => 'mybotname',
'nickname' => 'MyBot',
'password' => 'oauth:##########################'
);
echo 'Starting connection...';
$connection = array();
$connection['socket'] = fsockopen($settings['host'], $settings['port']);
if($connection['socket'])
{
echo 'Connected successfully';
transmitCommand("PASS " . $settings['password'] . "\n\r");
transmitCommand("NICK " . $settings['nickname'] . "\n\r");
transmitCommand("USER " . $settings['nickname'] . "\n\r");
transmitCommand("JOIN " . $settings['room'] . "\n\r");
while(!feof($connection['socket']))
{
echo 'Listening for messages...';
}
}
}
function transmitCommand($message)
{
global $connection;
fwrite($connection['socket'], $message, strlen($message));
echo "[SENT] $message <br>";
}
connectToIRC();
}
createTwitchBot();
?>
The connection attempt fails and I’m not sure what’s causing the problem. Has anyone successfully connected to Twitch IRC using PHP? Any suggestions on what might be wrong with my implementation would be really helpful!