Jump to content

IRC chat bot in PHP

Joveice

So I want to create a chat bot to use with Twitch. The bot gonna be used to get data from my website and display player ranks and so on. I was thinking to do it in Python since thats the only place I could fine start code to use as I know little to nothing about it.

 

But is it possible to do this in PHP? as that's a language I know a bit from before. Can I have it to join multiple channels? This is something I need. if I need to get extensions can I enable it for this only?

 

The reason I wanna leave Python is that I don't wanna learn 4 languages at a time.

 

Open for all suggestions. I was originaly gonna create the bot in mIRC but thats only windows and gui based, I need it to work for linux and no gui.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

I have no clue about Twitch, but I suspect it uses the IRC protocol (at least that's what the topic suggests), so here are my thoughts:

Pseudo-code:

if (($fp = fsockopen($myircserver, 6667)) != false) {
	while (1) {
		if (substr(fgets($fp), 0, 4) == "PING") {
			// send PONG or you'll be kicked
		}
		fwrite($fp, "JOIN foobar");
	}
}

Please read up the appropriate commands yourself. I happen to have an unfinished IRC client written in C here, but I assume C wouldn't help you much.

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

Sure. You'll need to work out some way to keep the socket persistently open, whether your webserver can do that for you, or whether you're happy running it as a console script, I don't know... I'm not a PHP programmer. I do know, there are plenty of existing nice bots, though...

 

Connecting via SSL is just... the typical SSL-style socket wrapper, like anything else. Thus, it might be best if you have a function that takes an already-connected socket and runs the IRC loop on it. As you can see, I've done this before: https://github.com/Sebbyastian/ircsock/blob/master/bot.mrc... Notice the "raw" commands "on connect" at line 8 and 9; they're your first steps. Make sure those initial lines are newline terminated (the spec says IRC uses carriage return+linefeed, but you should be tolerant of servers which only use a linefeed... or militant hehe, I like that way too!).

Note how I've used regex to parse IRC (<https://github.com/Sebbyastian/ircsock/blob/master/bot.mrc#L79>); unlike HTML you can safely parse most of IRC this way. I added the regular expressions to a table corresponding to an "anonymous function" (well, mIRC doesn't have those, but close enough), when the line matches the regex the function gets executed. It's simple and extendable, which is what you'll want for a bot.

noop $ast_add($1,PING( .*)?,raw PONG $!regml(1))

 

When a line matching /^(?:PING( .*)?)$/ is received, the matched group ( .*) is appended to a PONG response... This is just as important as line 8 and 9, as if you don't send the correct (newline-terminated) response your connection won't make it past the initial authorisation notices.

breplace &r 0 10 13 10

 

 

I treat null characters as though they're newlines, though the RFC says they're protocol errors... I suppose the same is true of stray newlines (without paired carrage returns). It's just easier to replace '\r' and '\0' with '\n', then parse everything as though it's terminated with '\n', though this technically could expose my bot to XSS-style forged commands, I suppose... I don't think IRC is that important :) just do what you must, because it's a horrible protocol at the end of the day, anyway...

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×