(Resolved)Creating a chat bot in PHP CLI - not pulling any information

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,


$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');
		$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.

1 Like
PASS oauth:token\r\n
NICK username\r\n

no USER required. Login unsuccessful would suggest your oauth token is invalid or doesn’t match the username. Your code works fine for me (changing USER to NICK).


Thank you.
I have no idea why, but my oauth was different than when I generated it. Got the new oauth in and everything is working clean.

Also keep in mind that you must put \r\n at the end of every line of text you send to the server

1 Like

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