hubot-rivescript
Version:
Provides a RiveScript chatbot personality for Hubot.
72 lines (62 loc) • 2.42 kB
text/coffeescript
# Description:
# Provides a RiveScript chatbot for Hubot. This bot will respond to all
# messages directed at it by using a RiveScript brain.
#
# Dependencies:
# "rivescript": "^1.13.0"
#
# Configuration:
# HUBOT_RIVESCRIPT_BRAIN - The path to a folder containing RiveScript files
# (with the *.rive file extensions).
# HUBOT_RIVESCRIPT_PREFIX - If you don't want your Hubot to answer all
# messages using RiveScript, set this string to be a prefix, e.g. with
# the value "rs" a user must say "hubot rs hello" for the bot to answer
# their "hello" message.
# HUBOT_RIVESCRIPT_UTF8 - Boolean to enable UTF-8 mode, 1 or 0, default 1
#
# Commands:
# hubot [rs] <message> - Retrieve a response from RiveScript.
#
# Author:
# Noah Petherbridge, https://github.com/kirsle
RiveScript = require "rivescript"
module.exports = (robot) ->
# Configuration parameters.
debug = "#{process.env.HUBOT_RIVESCRIPT_DEBUG}" is "1" or false
brain = process.env.HUBOT_RIVESCRIPT_BRAIN or "./brain"
prefix = if process.env.HUBOT_RIVESCRIPT_PREFIX then "#{process.env.HUBOT_RIVESCRIPT_PREFIX}\\s+" else "\\s*"
utf8 = "#{process.env.HUBOT_RIVESCRIPT_UTF8}" is "1" or false
sync = "#{process.env.HUBOT_RIVESCRIPT_SYNC}" is "1" or false
# The matcher regexp.
regexp = new RegExp("#{prefix}(.*)", "i")
# Initialize the RiveScript brain.
# Save user variables after each interaction.
userData = robot.brain.get("rivescript") or {}
userData[user.name] = @bot.getUservars(user.name)
robot.brain.set("rivescript", userData)