Sinbotra

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file lib/sinbotra. To experiment with that code, run bin/console for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

Installation

Add this line to your application's Gemfile:

gem 'sinbotra'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sinbotra

Usage

TODO: Write usage instructions here

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sinbotra. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

New API!

You take an incoming message

Create a user out of the sender and a bot out of the user

You tell the bot to handle the message

It checks if the user is in a conversation

If YES then handle the current conversation step. On step end, increase conversation step or end convo

If NO then cycle through handlers

If still nothing, answer with default if it exists

If still not, use default I don't understandj

class BurpeeBot < Sinbotra::Bot
  listeners onboard:       /hi|hello/,
            start_workout: /start|begin/

  default_listener :default

  conversations onboarding:    OnboardingConversation,
                start_workout: StartWorkoutConversation

  actions start_workout: StartWorkoutAction

  def onboard(msg, user)
    bot.say(user, "Hello #{user.name}")
    start_conversation(:onboarding)
  end

  def start_workout(msg, user)
  end

  def default(msg, user)
  end
end

BurpeeBot.perform_action(:start_workout, args)

class StartWorkoutAction
  def call(bot, args={})
    athletes = Athlete::Repo.find_fresh
    athletes.each do |athlete|
      bot.send_text(athlete.fb_id, "Time to work out!!!")
    end
  end
end

class Sinbotra::Conversation
  class << self
    attr_reader :step_names
    def steps(*all_steps)
      @step_names = all_steps
    end
  end

  attr_reader :current_user
  attr_reader :step_num

  def initialize(user, bot)
    @current_user = user
    @step_num = 0
    @active   = true
    @steps    = self.class.step_names
  end

  def next_step!
    @step_num += 1
    if @step_num == @steps.size
      end_conversation!
    end
  end

  def say(txt)
    STDOUT.puts(txt)
  end

  def end_conversation!
    @step_num = -1
    @active   = false
  end

  def active?; @active; end
end

class OnboardingConversation
  include Sinbotra::Conversation

  steps :ask_name, :ask_age, :ask_fitness_level, :all_good

  def ask_name(msg)
    say("Hello, what is your name?")
    # I have access to current_user
    next_step!
  end

  def ask_age(msg, bot)
    say("Great, your name is #{msg.text}")
    say("How old are you?")
    next_step!
  end

  def ask_fitness_level(msg, bot)
    say("Great, your #{msg.text} years old.")
    say("What's your fitness level?")
    next_step!
  end

  def all_good(msg, bot)
    say("Great, your fitness level is #{msg.text}.")
    say("We have everything we need!")
    end_conversation!
  end
end

class Bot::User
  attr_reader :id, :current_converation
end