User avatar
Theladdie
Posts: 23
Joined: Mon May 14, 2012 11:38 am
Location: Berlin, Germany
Contact: Website

Advice: Python IRC Bot

Tue Mar 05, 2013 7:06 pm

Hey guys,

I am building an IRC bot in python. This is more a project for me to learn python.

What I wanted to do is for now when I say 'bot: hello' it would respond (thats the simple command I planned). Of course I want to add more commands so I don't want to have everything in one file but rather make plugin files with addtional code. On top of that for the hello example there might be other triggers like 'hi' or 'yo' which would give the same response...

So my question is do you know what would be the best way to check triggers and then get them to a function/plugin file to execute the code.

User avatar
Theladdie
Posts: 23
Joined: Mon May 14, 2012 11:38 am
Location: Berlin, Germany
Contact: Website

Re: Advice: Python IRC Bot

Wed Mar 06, 2013 9:03 am

so to add, I don't want to use a frame work cause I prefer the deeper end (also I dabled with twisted and it did not really work out).

I think the only way is to check each plugin every time there is a message... But something tells me that is not efficient?

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: Advice: Python IRC Bot

Wed Mar 06, 2013 9:40 am

Plugins should register handlers for specific triggers. Then the main app can easily start them when a specific event occurs. There is a lot of stuff on stackoverflow and google; search for "python plugins".
pythonhosted.org/RPIO

User avatar
Theladdie
Posts: 23
Joined: Mon May 14, 2012 11:38 am
Location: Berlin, Germany
Contact: Website

Re: Advice: Python IRC Bot

Wed Mar 06, 2013 11:37 am

Thanks!

Funny what one word does to your search.

I found the following link and am starting to crowbar it into my bot : http://lkubuntu.wordpress.com/2012/10/0 ... lugin-api/

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Advice: Python IRC Bot

Wed Mar 06, 2013 12:22 pm

To implement the registration metachris mentioned, you could create a dictionary object to store the trigger+plugin tuples and each plugin could have a method to retrieve the triggers the plugin supports - something along:

Code: Select all

all_triggers = dict()
for i in pluginloader.getPlugins():
    print("Loading plugin " + i["name"])
    plugin = pluginloader.loadPlugin(i)
    #plugin.run()
    plugin_triggers = plugin.get_triggers()
    for trg in plugin_triggers:
        all_triggers[trg] = plugin
and to use

Code: Select all

irc_input = ...
if irc_input in all_triggers:
    plugin = all_triggers[irc_input ]
    plugin.run()
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

User avatar
Theladdie
Posts: 23
Joined: Mon May 14, 2012 11:38 am
Location: Berlin, Germany
Contact: Website

Re: Advice: Python IRC Bot

Tue Mar 26, 2013 1:11 pm

Yay

I sorta got it working

You can now see my butchered code here: https://bitbucket.org/theladdie/robinbot

Return to “Python”