lib/twitter_ebooks/bot.rb
# Connects to tweetstream and opens event handlers for this bot
Connects to tweetstream and opens event handlers for this bot
Connects to tweetstream and opens event handlers for this bot
def start
      configure
      @on_startup.call if @on_startup
      @stream.on_error do |msg|
        log "ERROR: #{msg}"
      end
      @stream.on_inited do
        log "Online!"
      end
      @stream.on_event(:follow) do |event|
        next if event[:source][:screen_name] == @username
        log "Followed by #{event[:source][:screen_name]}"
        @on_follow.call(event[:source]) if @on_follow
      end
      @stream.on_direct_message do |dm|
        next if dm[:sender][:screen_name] == @username 
        log "DM from @#{dm[:sender][:screen_name]}: #{dm[:text]}"
        @on_message.call(dm) if @on_message
      end
      @stream.userstream do |ev|
        next unless ev[:text] 
        next if ev[:user][:screen_name] == @username 
        meta = {}
        mentions = ev.attrs[:entities][:user_mentions].map { |x| x[:screen_name] }
        reply_mentions = mentions.reject { |m| m.downcase == @username.downcase }
        reply_mentions = [ev[:user][:screen_name]] + reply_mentions
        meta[:reply_prefix] = reply_mentions.uniq.map { |m| '@'+m }.join(' ') + ' '
        meta[:limit] = 140 - meta[:reply_prefix].length
        mless = ev[:text]
        begin
          ev.attrs[:entities][:user_mentions].reverse.each do |entity|
            mless = mless[0...entity[:indices][0]] + mless[entity[:indices][1]...-1]
          end
        rescue Exception
          p ev.attrs[:entities][:user_mentions]
          p ev[:text]
          raise
        end
        meta[:mentionless] = mless
        if mentions.map(&:downcase).include?(@username.downcase) && !ev[:retweeted_status] && !ev[:text].start_with?('RT ')
          log "Mention from @#{ev[:user][:screen_name]}: #{ev[:text]}"
          @on_mention.call(ev, meta) if @on_mention
        else
          @on_timeline.call(ev, meta) if @on_timeline
        end
      end
    end
def start configure @on_startup . call if @on_startup @stream . on_error do | msg | log "ERROR: #{msg}" end @stream . on_inited do log "Online!" end @stream . on_event ( :follow ) do | event | next if event [ :source ] [ :screen_name ] == @username log "Followed by #{event[:source][:screen_name]}" @on_follow . call ( event [ :source ] ) if @on_follow end @stream . on_direct_message do | dm | next if dm [ :sender ] [ :screen_name ] == @username log "DM from @#{dm[:sender][:screen_name]}: #{dm[:text]}" @on_message . call ( dm ) if @on_message end @stream . userstream do | ev | next unless ev [ :text ] next if ev [ :user ] [ :screen_name ] == @username meta = { } mentions = ev . attrs [ :entities ] [ :user_mentions ] . map { | x | x [ :screen_name ] } reply_mentions = mentions . reject { | m | m . downcase == @username . downcase } reply_mentions = [ ev [ :user ] [ :screen_name ] ] + reply_mentions meta [ :reply_prefix ] = reply_mentions . uniq . map { | m | '@' + m } . join ( ' ' ) + ' ' meta [ :limit ] = 140 - meta [ :reply_prefix ] . length mless = ev [ :text ] begin ev . attrs [ :entities ] [ :user_mentions ] . reverse . each do | entity | mless = mless [ 0 ... entity [ :indices ] [ 0 ] ] + mless [ entity [ :indices ] [ 1 ] ... - 1 ] end rescue Exception p ev . attrs [ :entities ] [ :user_mentions ] p ev [ :text ] raise end meta [ :mentionless ] = mless if mentions . map ( & :downcase ) . include? ( @username . downcase ) && ! ev [ :retweeted_status ] && ! ev [ :text ] . start_with? ( 'RT ' ) log "Mention from @#{ev[:user][:screen_name]}: #{ev[:text]}" @on_mention . call ( ev , meta ) if @on_mention else @on_timeline . call ( ev , meta ) if @on_timeline end end end
Connects to tweetstream and opens event handlers for this bot
Connects to tweetstream and opens event handlers for this bot
# Don't reply to self # If it's not a text-containing tweet, ignore it # Ignore our own tweets # To check if this is a mention, ensure: # - The tweet mentions list contains our username # - The tweet is not being retweeted by somebody else # - Or soft-retweeted by somebody else