Guest User

bots.rb

a guest
Apr 14th, 2015
210
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'twitter_ebooks'
  2.  
  3. # Information about a particular Twitter user we know
  4. class UserInfo
  5.   attr_reader :username
  6.  
  7.   # @return [Integer] how many times we can pester this user unprompted
  8.   attr_accessor :pesters_left
  9.  
  10.   # @param username [String]
  11.   def initialize(username)
  12.     @username = username
  13.     @pesters_left = 1
  14.   end
  15. end
  16.  
  17. class CloneBot < Ebooks::Bot
  18.   attr_accessor :original, :model, :model_path
  19.  
  20.   def configure
  21.     # Configuration for all CloneBots
  22.     self.consumer_key = ""
  23.     self.consumer_secret = ""
  24.     self.blacklist = ['kylelehk', 'friedrichsays', 'Sudieofna', 'tnietzschequote', 'NerdsOnPeriod', 'FSR', 'BafflingQuotes', 'Obey_Nxme']
  25.     self.delay_range = 1..6
  26.     @userinfo = {}
  27.   end
  28.  
  29.   def top100; @top100 ||= model.keywords.take(100); end
  30.   def top20;  @top20  ||= model.keywords.take(20); end
  31.  
  32.   # AddOn: don't use '#'
  33.   def checkTweet(tweetText)
  34.     if not tweetText =~ /#/
  35.         return true
  36.     end
  37.   end
  38.  
  39.   def on_startup
  40.     load_model!
  41.  
  42.     scheduler.cron '1h' do
  43.       genTweet = true
  44.       while genTweet do
  45.         tweetText = model.make_statement
  46.         if checkTweet(tweetText)
  47.             genTweet = false
  48.         end
  49.       end;
  50.      
  51.       tweet(tweetText)
  52.     end
  53.   end
  54.  
  55.   def on_message(dm)
  56.     delay do
  57.       # AddOn: don't use '#'
  58.       genTweet = true
  59.       while genTweet do
  60.         tweetText = model.make_response(dm.text)
  61.         if checkTweet(tweetText)
  62.             genTweet = false
  63.         end
  64.       end;
  65.      
  66.       reply(dm, tweetText)
  67.     end
  68.   end
  69.  
  70.   def on_mention(tweet)
  71.     # Become more inclined to pester a user when they talk to us
  72.     userinfo(tweet.user.screen_name).pesters_left += 1
  73.  
  74.     delay do
  75.       # AddOn: don't use '#'
  76.       genTweet = true
  77.       while genTweet do
  78.         tweetText = model.make_response(meta(tweet).mentionless, meta(tweet).limit)
  79.         if checkTweet(tweetText)
  80.             genTweet = false
  81.         end
  82.       end;
  83.       reply(tweet, tweetText)
  84.     end
  85.   end
  86.  
  87.   def on_timeline(tweet)
  88.     return if tweet.retweeted_status?
  89.     return unless can_pester?(tweet.user.screen_name)
  90.  
  91.     tokens = Ebooks::NLP.tokenize(tweet.text)
  92.  
  93.     interesting = tokens.find { |t| top100.include?(t.downcase) }
  94.     very_interesting = tokens.find_all { |t| top20.include?(t.downcase) }.length > 2
  95.  
  96.     delay do
  97.       if very_interesting
  98.         retweet(tweet) if rand < 0.1
  99.         favorite(tweet) if rand < 0.5
  100.         if rand < 0.01
  101.           userinfo(tweet.user.screen_name).pesters_left -= 1
  102.           # AddOn: don't use '#'
  103.           genTweet = true
  104.           while genTweet do
  105.             tweetText = model.make_response(meta(tweet).mentionless, meta(tweet).limit)
  106.             if checkTweet(tweetText)
  107.                 genTweet = true
  108.             end
  109.           end;
  110.           reply(tweet, tweetText)
  111.         end
  112.       elsif interesting
  113.         favorite(tweet) if rand < 0.05
  114.         if rand < 0.001
  115.           userinfo(tweet.user.screen_name).pesters_left -= 1
  116.           # AddOn: don't use '#'
  117.           genTweet = true
  118.           while genTweet do
  119.             tweetText = model.make_response(meta(tweet).mentionless, meta(tweet).limit)
  120.             if checkTweet(tweetText)
  121.                 genTweet = true
  122.             end
  123.           end;
  124.           reply(tweet, tweetText)
  125.         end
  126.       end
  127.     end
  128.   end
  129.  
  130.   # Find information we've collected about a user
  131.   # @param username [String]
  132.   # @return [Ebooks::UserInfo]
  133.   def userinfo(username)
  134.     @userinfo[username] ||= UserInfo.new(username)
  135.   end
  136.  
  137.   # Check if we're allowed to send unprompted tweets to a user
  138.   # @param username [String]
  139.   # @return [Boolean]
  140.   def can_pester?(username)
  141.     userinfo(username).pesters_left > 0
  142.   end
  143.  
  144.   # Only follow our original user or people who are following our original user
  145.   # @param user [Twitter::User]
  146.   def can_follow?(username)
  147.     @original.nil? || username == @original || twitter.friendship?(username, @original)
  148.   end
  149.  
  150.   def favorite(tweet)
  151.     if can_follow?(tweet.user.screen_name)
  152.       super(tweet)
  153.     else
  154.       log "Unfollowing @#{tweet.user.screen_name}"
  155.       twitter.unfollow(tweet.user.screen_name)
  156.     end
  157.   end
  158.  
  159.   def on_follow(user)
  160.     if can_follow?(user.screen_name)
  161.       follow(user.screen_name)
  162.     else
  163.       log "Not following @#{user.screen_name}"
  164.     end
  165.   end
  166.  
  167.   private
  168.   def load_model!
  169.     return if @model
  170.  
  171.     @model_path ||= "model/#{original}.model"
  172.  
  173.     log "Loading model #{model_path}"
  174.     @model = Ebooks::Model.load(model_path)
  175.   end
  176. end
  177.  
  178. CloneBot.new("xxx_ebooks") do |bot|
  179.   bot.access_token = ""
  180.   bot.access_token_secret = ""
  181.  
  182.   bot.original = "xxx_ebooks"
  183. end
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×