lib/chatterbot/helpers.rb in chatterbot-0.2.8 vs lib/chatterbot/helpers.rb in chatterbot-0.2.9
- old
+ new
@@ -1,29 +1,55 @@
module Chatterbot
#
# a bunch of helper routines for bots
module Helpers
+ def botname=(b)
+ @botname = b
+ end
#
# The name of the currently running bot
def botname
- if self.class < Bot
+ if !@botname.nil?
+ @botname
+ elsif self.class < Bot
self.class.to_s.downcase
else
File.basename($0,".rb")
end
end
#
+ # Pull the username from a tweet hash -- this is different depending on
+ # if we're doing a search, or parsing through replies/mentions.
+ def from_user(s)
+ return s if s.is_a?(String)
+ s.has_key?(:from_user) ? s[:from_user] : s[:user][:screen_name]
+ end
+
+
+ #
# 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.
def tweet_user(tweet)
- if ! tweet.is_a?(String)
- base = tweet.has_key?(:from_user) ? tweet[:from_user] : tweet[:user][:screen_name]
+ base = from_user(tweet)
+ base =~ /^@/ ? base : "@#{base}"
+ end
+
+
+ #
+ # 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
+ #
+ def replace_variables(txt, original = nil)
+ if ! original.nil? && txt.include?("#USER#")
+ username = tweet_user(original)
+ txt.gsub("#USER#", username)
else
- base = tweet
+ txt
end
- base =~ /^@/ ? base : "@#{base}"
end
+
end
end