Sha256: 28db4ae6832045d1ec73867fe84bab75e0699868c642f60d0d13f4f321ee1443

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

#!/usr/bin/env ruby

require 'chatterbot/dsl'

##
#
# Simple example of a bot that will log tweets to the
# database. Chatterbot will only log outgoing tweets by default, and
# coding incoming tweet processing seems problematic right now, but
# this is pretty straightforward
#

#
# Set some date defaults for Sequel
#
Sequel.datetime_class = DateTime
Sequel.default_timezone = :utc

#
# grab a copy of the db handle
#
db = bot.db

#
# create a table to hold search results
#
if ! db.tables.include?(:searches)

  #
  # if there's other data you want to track, you can add it here
  #
  db.create_table :searches do
    primary_key :id, :type=>Bignum

    String :text
    String :from_user
    String :from_user_id

    String :to_user
    String :to_user_id

    String :in_reply_to_status_id

    DateTime :created_at
  end
end

cols = db[:searches].columns

#
# run a search
#
search("foo", :lang => "en") do |tweet|

  #
  # reject anything from the incoming tweet that doesn't have a
  # matching column
  #
  data = tweet.to_h.delete_if { |k, v|
    ! cols.include?(k)
  }

  # update timestamp manually -- sequel isn't doing it right
  data[:created_at] = Sequel.string_to_datetime(data[:created_at])

  # store to the db!
  db[:searches].insert(data)
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
chatterbot-2.0.3 examples/tweet_logger.rb
chatterbot-2.0.2 examples/tweet_logger.rb
chatterbot-1.0.2 examples/tweet_logger.rb
chatterbot-2.0.0.pre examples/tweet_logger.rb
chatterbot-1.0.1 examples/tweet_logger.rb
chatterbot-1.0.0 examples/tweet_logger.rb