Module: Chatterbot::Helpers

Included in:
Bot
Defined in:
lib/chatterbot/helpers.rb

Overview

a bunch of helper routines for bots

Instance Method Summary collapse

Instance Method Details

#botnameObject

The name of the currently running bot



17
18
19
20
21
22
23
24
25
# 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) ⇒ Object

Set the username of the bot. This is used when generating config/skeleton file during registration



11
12
13
# File 'lib/chatterbot/helpers.rb', line 11

def botname=(b)
  @botname = b
end

#current_userObject

find the user of the current tweet/object we are dealing with



68
69
70
71
72
# 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) ⇒ Object

Pull the username from a tweet hash – this is different depending on if we’re doing a search, or parsing through replies/mentions.



30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object

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



56
57
58
59
60
61
62
63
# 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) ⇒ Object

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.



45
46
47
48
# File 'lib/chatterbot/helpers.rb', line 45

def tweet_user(tweet)     
  base = from_user(tweet)
  base =~ /^@/ ? base : "@#{base}"
end