module Chatterbot::Helpers
a bunch of helper routines for bots
Public Instance Methods
botname()
click to toggle source
The name of the currently running bot
# File lib/chatterbot/helpers.rb, line 17 def botname if !@botname.nil? @botname elsif self.class < Bot self.class.to_s.downcase else File.basename($0,".rb") end end
botname=(b)
click to toggle source
Set the username of the bot. This is used when generating config/skeleton file during registration
# File lib/chatterbot/helpers.rb, line 11 def botname=(b) @botname = b end
current_user()
click to toggle source
find the user of the current tweet/object we are dealing with
# File lib/chatterbot/helpers.rb, line 68 def current_user return nil if @current_tweet.nil? return @current_tweet.sender if @current_tweet.respond_to?(:sender) return @current_tweet.user end
from_user(s)
click to toggle source
Pull the username from a tweet hash – this is different depending on if we're doing a search, or parsing through replies/mentions.
# File lib/chatterbot/helpers.rb, line 30 def from_user(s) case s when Twitter::Tweet s.user.screen_name when Twitter::User s.name when String s end end
replace_variables(txt, original = nil)
click to toggle source
do some simple variable substitution. for now, it only handles replacing #USER# with the screen of the incoming tweet, but it could do more if needed
# File lib/chatterbot/helpers.rb, line 56 def replace_variables(txt, original = nil) if ! original.nil? && txt.include?("#USER#") username = tweet_user(original) txt.gsub("#USER#", username) else txt end end
tweet_user(tweet)
click to toggle source
Take the incoming tweet/user name, and turn it into something suitable for replying to a user. Basically, get their handle and add a '@' to it.
# File lib/chatterbot/helpers.rb, line 45 def tweet_user(tweet) base = from_user(tweet) base =~ /^@/ ? base : "@#{base}" end