Simply put, I’m starting from the bottom up, building a PHP chatbot. Right now, the only thing I’m trying to get it to do, is connect, join a channel, and take all messages as received and just dump it all into a file, no more, no less.
Since, although I understand the basics of IRC, I’d like to see what a raw chatlog looks like, in order to figure out just how i want to parse it.
From what I see from here: http://help.twitch.tv/customer/en/portal/articles/1302780
I don’t see what I’m doing wrong.
After an fsockopen(), I attempt to log in. The link shows PASS and NICK being used, but when I use that, I get an error “:tmi.twitch.tv NOTICE * :Login unsuccessful” when I use PASS and USER as you’ll see in my script, it throws no errors, but I can’t seem to even pull the MOTD. Because I do get the error message, I know that fgets() is working,
<?php
set_time_limit(0);
$chan = "#deciblebreaker";
$server = "irc.chat.twitch.tv";
$port = 6667;
$nick = "deciblebreaker";
$pass = "oauth:******************************"; // actual oauth is used in script.
$socket = fsockopen($server, $port,$errno,$errstr,15);
echo $errno."\n".$errstr."\n"; // echoes "0 and no string. no errors connecting
fwrite($socket,"PASS ".$pass."\r\n"); // have tried PASS after USER/NICK, no changes
fwrite($socket,"USER ".$nick."\r\n"); // have tried both fwrite and fputs. no difference in results.
//fwrite($socket,"NICK ".$nick."\r\n");
// have tried NICK instead of USER, as well as both NICK and USER, and visa versa. still errors ":tmi.twitch.tv NOTICE * :Login unsuccessful"
// will only not error if only USER is sent.
echo var_dump($socket); // resource(4) of type (stream) - what one would expect from an open socket.
fwrite($socket,"JOIN ".$chan."\r\n"); // have also tried sending all commands without "\r", only "\n" character. makes no difference
while(1) {
echo "starting pull loop\n"; // prints on command prompt once
while($data = fgets($socket,256)) { // Script never gets past this point, unless login error, then it does 1 time.
echo $data; // does not print unless login error
$file = fopen("./testfile.txt",'a');
fwrite($file,$data."\r\n");
fclose($file);
$ex = explode (' ', $data);
if($ex[0] == "PING"){
fputs($socket, "PONG ".$ex[1]."\r\n");
}
}
}
?>
And yes, I’m aware that the code is far from ideal. I know I’m opening and closing the dumpfile repeatedly, and there is a reason it’s being done like that for now. At the moment, all I want it to do is pull and write. I am completely clueless as to what is preventing me from pulling data from the channel. no MOTD, no chat from the channel, nothing.