View Full Version : Generic Dilemma
lavinpj1
06-06-2006, 06:52 PM
Not too sure as of the best way to approach this, so any advice would be good...
I am just about to start developing a PHP IRC bot system. The events for the bot will be stored in a database, and on every line of text in the IRC Channel (chat room) the bot is in, it must check if it complies with one of these events. For example an event may be trigered by the text "hello" and the bot would respond with an action.
The problem I am having is that I cannot decide on the best way to check each line of text against these events. I do not really want to do an sql query on each line at risk of killing the server. My other idea was that I could load the whole table into an array on load of the bot, and just use in_array to search it. But, the problem with this is that I need to do regex matches on each event, e.g. check the event ".*hello.*" against an inputted line of text, e.g. "hi hello hi".
What do we think is the fastest and less server-munching way of searching for this?
Any help would be appreciated
~Phil~
how many people do you think would be triggering events?
how frequently would events be triggered?
how many different words would trigger an event?
also, you could use a flat file and strpos.
as you stated, you would need need to split the words from IRC by non word characters, preg_split('/\W+/') or get maybe, preg_match('/(\w+)/', $some_string, $matches)
lavinpj1
06-06-2006, 10:45 PM
The events would be have to be checked on every line of text. Comparing them isn't a problem, it's a case of how is best to compare them, whether I check straight from the database using LIKE or whether I just do a loop through an array and do a preg_match.
~Phil~
marek_mar
06-06-2006, 10:51 PM
Don't check them from a database.
Try not to use regex. Best would be if you would do as normal irc bots that react only if you start you input with a "!" or another special charecter.
The events would be have to be checked on every line of text
right, but what is the volume of lines? 1 line per second? 1 line per 10 seconds? are you going to fire a complete new event for each one that occurs or will it be queued up FIFO-style?
Comparing them isn't a problem, it's a case of how is best to compare them, whether I check straight from the database using LIKE or whether I just do a loop through an array and do a preg_match.
well, it depends on how much data you plan on dealing with which I tried to get at in my earlier post.
and, multiple events can be triggered from a line?
I'd say you'd you want to cache the 'dictionary' of words if the amount is small. For example, store it as an array in a flat file which you will eval() then use a combination of array_map/preg_match. I believe in_array is case sensitive so you wouldn't use that unless case did matter. if you plan on your dictionary being quite large, you could do a match against a table with like(like is not case sensitive for MySQL).
lavinpj1
06-06-2006, 11:01 PM
All irc bots can be coded to do as you please. People just code them to use ! at the start of commands for simplicity.
~Phil~
lavinpj1
06-06-2006, 11:03 PM
This is going to be a public system fci. I cannot know how many entries people are going to have, or whether it will sit in a channel with 1 line per hour or 60 lines per min.
~Phil~
no, not just for 'simplicity.' a bot should be commanded instead of arbitrarily receiving commands from sentences that have been sent out (which is more random).
although we don't know your intentions so your needs may be different.
if you want a normal/practical IRC bot, you should listen to marek_mar
maybe you shoudl give more context about the purpose of the bot and we would offer advice that suits your needs
lavinpj1
06-06-2006, 11:25 PM
http://phiplan.com
I wasn't asking what bot you were using, I wanted to know what you would be using it for and its purpose. is it to be a support bot? a spam bot? a quiz bot?... etc ?
lavinpj1
06-07-2006, 06:18 AM
LOL! That is the bot I am coding. As I said before, it is a public project. I cannot know what users will want to do.
~Phil~
I think you need to start by doing a bit of research/guessing as to what it's likely to be used for, and go from there. The original question is far too ambiguous.
Curtis D
06-07-2006, 09:39 AM
If your bot is going to be so general purpose, you might want to add configuration options to choose to use the database or flat file method like how fci was describing.
LOL! That is the bot I am coding. As I said before, it is a public project. I cannot know what users will want to do.
~Phil~
then you should make it like every other bot and follow what marek_mar said. maybe have a way to manage the allowed bot commands and so on.
marek_mar
06-07-2006, 01:23 PM
What I said about the special "!" character was based on my IRC experience. People usually get annoyed when a bot says something that they don't want becouse 1) They don't want it and 2) It is most likely useless information.
Secondly the "!" was an example. It should be configurable since popular bots like ChanServ use that character and you might end up in some sort of conflict.
lavinpj1
06-07-2006, 01:30 PM
I think you need to start by doing a bit of research/guessing as to what it's likely to be used for, and go from there. The original question is far too ambiguous.
Every project has specifications, mine is that it is general purpose and must allow the user to add any type of commands. I cannot just modify that.
If your bot is going to be so general purpose, you might want to add configuration options to choose to use the database or flat file method like how fci was describing.
That was something that crossed my mind also. It is certainly possible to do that and possibly a good solution.
then you should make it like every other bot and follow what marek_mar said. maybe have a way to manage the allowed bot commands and so on.
That is certainly not true. I have made many bots in my time and not all of them use commands with a ! at the start. For example, on one, a while ago, I had a command *slaps*. This checked if the third word was the bots nickname, e.g. in the line of text "jim slaps botty" and the bot would respond with a slap back. In mIRC script...
on 1:TEXT:*slaps*:#:{
if ($3 == $me) {
/describe $chan slaps $nick back with a hammer
}
}
As I said above, these are my specifications and they cannot be changed.
~Phil~
so the logic follows this?
1. check string against pattern
2. find actions that respond to that type of pattern
3. execute action
flat file way would potentially be less of a pain (easier to edit and no mysqld).. it is likely you would need a web frontend if you chose to use mysql.
i'm not realy sure what all these discussions are about, but if i understand your questions correctly, then you want suggestions on:
- how to detect if a line contains a keyword (can be any sort of string, without a fixed pattern like 'needs to start with a !');
- what's then the most performant and maintainable way to select the action that needs to be performed.
am i correct?
lavinpj1
06-07-2006, 03:32 PM
flat file way would potentially be less of a pain (easier to edit and no mysqld).. it is likely you would need a web frontend if you chose to use mysql.
The whole point of the bot is that it has a management interface to add events and actions on those events. The flat file idea may perhaps be better, but how would I regex search it?
~Phil~
The whole point of the bot is that it has a management interface to add events and actions on those events. The flat file idea may perhaps be better, but how would I regex search it?
~Phil~
to search a file, could do something like this:
<?php
function your_callback($element) {
if (preg_match('/2/', $element, $matches))
return $element;
}
$your_file = 'test.txt';
//$lines = file($your_file);
// example array for $lines:
$lines = array('test', 'test2');
$result = array_filter($lines, 'your_callback'); // you can use create_function to be more dynamic
var_dump($result);
?>
if you want a management interface, go with mysql, if that turns out to be ineffecient, you could always have it dump to a flat file to cache it then have it scan through the files.
lavinpj1
06-07-2006, 04:11 PM
if you want a management interface, go with mysql, if that turns out to be ineffecient, you could always have it dump to a flat file to cache it then have it scan through the files.
Hrmmmm, ok, what I THINK i'm going to do is to combine all of your suggestions. I will give an option to either dump the database to an array on load and loop through it doing a regex match, or to access directly from the database. This will give the user control over the server load and efficiency of the bot.
Any other ideas would be appreciated
~Phil~
actually, I take back the previous post and suggest preg_grep instead for searching an array with a pattern
http://us3.php.net/manual/en/function.preg-grep.php
lavinpj1
06-07-2006, 06:16 PM
AHA! Perfect! Thanks very much fci.
That looks exactly what I need. I feel now, the fastest way would be to give users the option of selecting directly from a database if they want to; but, as default, to load the whole table into an array on bot startup, and do the preg_grep on it. I could also perhaps add a quick check sequence on private messages, which checks for a "!reload password_here" command (yes, with a ! for simplicity and definition from other pms :P). This would reload the array with current table data. This would avoid having to restart the bot whenever data was changed.
I have lots of feature ideas in my mind about this project, and i'm sure i'll be posting again for views on the best way to approach them.
I appologise for being so vaige in the initial stages also.
Thanks again
~Phil~
Curtis D
06-09-2006, 01:12 AM
Glad to hear you've found a solution. Best of luck. I've just recently learned how to use IRC on the client end, so I may end up venturing to learn the IRC protocol sooner or later.
Glad to hear you've found a solution. Best of luck. I've just recently learned how to use IRC on the client end, so I may end up venturing to learn the IRC protocol sooner or later.
here is a starting point:
http://www.irchelp.org/irchelp/rfc/
lavinpj1
06-09-2006, 02:09 PM
The IRC protocol is nice. Simple yet effective. Very easy to pick up once you have used a standard client for a few weeks and have got to grips with the things you can do.
~Phil~
Curtis D
06-10-2006, 07:24 AM
Thanks guys. That link helped out a lot, it seems like it has all the info I will need, and RFCs, of course, are authoritative. I'll have to share the fruit of my research down the line! :D
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.