-
Notifications
You must be signed in to change notification settings - Fork 12
Writing Plugins
pbrisbin edited this page Jan 11, 2013
·
1 revision
Create a new file at plugins/my_plugin.rb
and fill it with one (or more) or the following bits of code:
# plugin will execute on any messages sent to basil
# with text matching the regular expression
Basil.respond_to(/a regex/) do
# ...
end
# plugin will execute on any messages sent to anyone
# with text matching the regular expression
Basil.watch_for(/a regex/) do
# ...
end
# plugin will execute on any message basil receives via
# email (if so configured) with a subject that matches
# the regular expression
Basil.check_email(/a regex/) do
# ...
end
The blocks you define are executed whenever a matching message is encountered.
During execution:
-
self
is the instance ofPlugin
executing -
@msg
is the instance ofMessage
which triggered execution -
@match_data
is an instance ofMatchData
created when your regular expression was matched against the message.
Understanding this, the following example should be enough to get you going:
Plugin.respond_to(/^google (.+)/) do
result = get_json("http://google.com?format=json&q=#{escape(@match_data[1])}").first
@msg.say "#{result['title']}: #{result['url']}"
end
Note: there are simplifications in the example, see plugins/google.rb
for the real implementation.
-
respond_to('string')
is a shorthand forrespond_to(/^string$/)
-
respond_to(/.../) { ... }.description = "Something"
will set a description to be used by thehelp
plugin.