Menu

Conditional_Plugin

Anonymous nanotube Valentin Lorentz Axanon

The Conditional plugin allows you to execute the equivalent of "if-then" statements in the bot. Using command nesting, you can test conditions of arbitrary complexity. This is useful if you want a varying bot response depending on some other inputs (e.g., time of day, topic, weather in some location, etc.)

The plugin has a number of commands for logical operations to help out with the task.

The code for this plugin lives in the git repository of the gribble sourceforge project.

Logic

Conditional.cif

The cif command is the main workhorse. It would have been named just 'if', but it's not a good idea to name python functions after reserved keywords. :) The function takes three arguments, a boolean condition, command to execute when boolean is true, and command to execute when boolean is false.

Here's a simple example:

cif true "echo i'm true" "echo i'm false"

This would result in output of "i'm true".

This is of course not very useful... so let's use command nesting to our advantage, along with the other functions from this plugin:

cif [cand [nle [echo $hour] 19] [nge [echo $hour] 8]] "echo it is daytime!" "echo it is nighttime!"

Which has the bot claiming it is daytime between 0800 and 1900 hours, and nighttime otherwise.

cif [match "Conditions: Clear" [weather 10010]] "echo weather is good" "echo weather is not so good"

Will have your bot talk about the weather. (This example is using MTughan's WunderWeather plugin.

Conditional.cand

Takes a list of booleans, and evaluates whether all of them are true.

cand true true true

Would return 'true'.

Use command nesting with other functions from this plugin to your advantage.

Conditional.cor

Takes a list of booleans, and evaluates whether at least one of them is true.

cor false true true

would return 'true'.

Conditional.cxor

Takes a list of booleans, and evaluates whether one and only one of them is true.

cor false true true

would return 'false'.

String comparison

The following string comparison functions, each taking two string arguments, are available:

  • ceq - True if arg1 is equal to arg2
  • ne - True if arg1 is not equal to arg2
  • lt - True if arg1 is less than arg2
  • le - True if arg1 is less than or equal to arg2
  • gt - True if arg1 is greater than arg2
  • ge - True arg1 is greater than or equal to arg2
  • match - True if arg1 is a substring of (is contained in) arg2

Use these to your advantage as nested commands with cif.

Numeric comparison

The following numeric comparison functions, each taking two numeric arguments, are available:

  • nceq - True if arg1 is equal to arg2
  • nne - True if arg1 is not equal to arg2
  • nlt - True if arg1 is less than arg2
  • nle - True if arg1 is less than or equal to arg2
  • ngt - True if arg1 is greater than arg2
  • nge - True arg1 is greater than or equal to arg2

Use these to your advantage as nested commands with cif.

Some examples

See also: [MessageParser_Plugin]

Tell someone

@alias add sayhi "echo [cif [nlt 0 [len @1]] \"echo @1:\" \" echo \\\"\\\"\"] \"Hi\""

"@sayhi" will display " Hi" and "@sayhi somebody" will display "somebody: Hi"

FIXME: remove the prefix space in the first output.


Related

Wiki: Gribble_Project_Git_Repository
Wiki: MessageParser_Plugin

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.