Search code examples
phpbotsirc

Sending bold/colored messages on IRC through a PHP bot


I have a slightly special PHP bot of my own design which has only one very simple purpose : It reads the contents of a one line .txt file and writes it on IRC. Nothing more, nothing less.

This is how the bot reads the file then writes to IRC :

$custom_message = file_get_contents($file_path);
fputs($irc_socket,$custom_message."\r\n");

The code is that simple, there's nothing else to it, it's just those two lines running in a loop. This is working perfectly so far. It executes all the commands I want, using privmsg or whatever else.

My issue is that I can not get the bot to write messages with bold or colors in them. Here are the ways I attempted which didn't work :

privmsg Nickname 0x02test
privmsg Nickname \u0002test
privmsg Nickname \0x02test
privmsg Nickname \002test
privmsg Nickname \x035test
privmsg Nickname \x02test

All of these send a message to Nickname containing exactly what is written in them, non bolded.

I must be missing something here, maybe it's file_get_contents() or fputs() escaping characters in ways I'm not figuring out, perhaps something going on with the encoding of my .txt file (currently utf-8, tried others), or perhaps me just inputting the wrong codes to get bold or colors to work ?

Hopefully someone knows !


Thanks in advance,
Eric B.


Solution

  • file_get_contents is reading the file byte by byte and then you are writing those bytes to the socket. It's not interpreting your escape sequences.

    If you want to send the actual 0x02 byte you will have to have that byte in the file.

    In PHP you can write it by using fputs($fp, chr(0x02)).