Skip to content
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:

  1. self is the instance of Plugin executing
  2. @msg is the instance of Message which triggered execution
  3. @match_data is an instance of MatchData 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.

Additional tips

  • respond_to('string') is a shorthand for respond_to(/^string$/)
  • respond_to(/.../) { ... }.description = "Something" will set a description to be used by the help plugin.
Clone this wiki locally