progapandist / coordinator-bot

A tutorial that builds a simple bot for Facebook Messenger with Ruby and Sinatra
16 stars 6 forks source link

Bot only serves one person at a time #5

Open segun-adeleye opened 6 years ago

segun-adeleye commented 6 years ago

I followed your tutorial to set up my bot, but somehow, my bot can only serve one person at a time. If two users are chatting with the bot at the same time, their responses clash.

I have read through the facebook-messenger gem documentation but couldn't figure it out. Please what can I do to fix this problem.

Thanks

progapandist commented 6 years ago

Hi! This is indeed the case, look at the Rubotnik gem that I created once I realized the problem. https://github.com/progapandist/rubotnik On Wed 9 May 2018 at 08:28, Oluwasegun Adeleye notifications@github.com wrote:

I followed your tutorial to set up my bot, but somehow, my bot can only serve one person at a time. If two users are chatting with the bot at the same time, their responses clash.

I have read through the facebook-messenger gem documentation but couldn't figure it out. Please what can I do to fix this problem.

Thanks

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/progapandist/coordinator-bot/issues/5, or mute the thread https://github.com/notifications/unsubscribe-auth/AL7wnOmRojU5XpwJpEFvgezS7r6TrPrxks5twn5hgaJpZM4T3uy5 .

--

Best regards / Bien cordialement / С уважением, Andrei BARANOV / Андрей Баранов +33648241991 - FR +79031365092 - RU

segun-adeleye commented 6 years ago

Is this a general problem with facebook-messenger or the implementation? And does your gem solve the problem?

progapandist commented 6 years ago

Yes, there was a problem with the implementation, but Rubotnik solves the problem of multiple users, it also has a better readme and more features than this basic example On Wed 9 May 2018 at 22:05, Oluwasegun Adeleye notifications@github.com wrote:

Is this a general problem with facebook-messenger or the implementation? And does your gem solve the problem?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/progapandist/coordinator-bot/issues/5#issuecomment-387843252, or mute the thread https://github.com/notifications/unsubscribe-auth/AL7wnIAedIAthSWp_hDam-tLPLpev0Srks5twz4BgaJpZM4T3uy5 .

--

Best regards / Bien cordialement / С уважением, Andrei BARANOV / Андрей Баранов +33648241991 - FR +79031365092 - RU

segun-adeleye commented 6 years ago

Okay. But for learning purpose, how can I avoid such issue with facebook-messenger gem?

progapandist commented 6 years ago

You should have some sort of the user functionality that you need to write, Rubotnik does exactly that, you can just take a look at the source code and implement it in your own solution. Or, nest everything inside the single Bot.on block (that leads to nasty code, so Rubotnik also proposes a solution by providing DSL on top of facebook-messenger gem)

segun-adeleye commented 6 years ago

I am using the bot in a rails application and It seems your gem – Rubotnik – does not work with rails.

progapandist commented 6 years ago

No, it does not, not out of the box at least. Then I recommend making sure you never call Bot.on more then once per single message — and all the logic should be nested inside a single call.

segun-adeleye commented 6 years ago

What I eventually did was to save a reference to the user and the next command to be executed on every Bot.on :message. I had something like

class MyBot
  class << self
    def listen
      Bot.on :message do |message|
        sender_id = message.sender['id']

        if  @@command[sender_id].present?
          command = @@command[sender_id]

          args = [message] + command[1]
          method(command[0]).call(*args)
        else
          # Start of Conversation
          start_conversation(message)
        end
      end
    end

    def start_conversation(message)
      message.reply(text: 'You welcome')
      next_command message.sender['id'], :go_to_next_thing
    end

    def go_to_next_thing(message)
      message.reply(text: 'Say something!')
      next_command message.sender['id'], :end_conversation
    end

    def end_conversation(message)
      # End of the conversation
      message.reply(text: 'We are done!')
      @@command.delete [message.sender['id']]

      listen
    end

    def next_command(sender_id, command, args = [])
      @@command[sender_id] = [command, args]
      listen
    end
  end
end

MyBot.listen
progapandist commented 6 years ago

Nice! That's exactly what Rubotnik does :) Basically, after I realized I have a problem with concurrent users, I wrote another gem

progapandist commented 6 years ago

There's an article about it here https://hackernoon.com/ya-tvoy-rubotnik-beginner-friendly-ruby-boilerplate-to-build-your-own-messenger-bot-9e5fc35ee53b

segun-adeleye commented 6 years ago

Alright. By the way, what do you have to do to make it work in a Rails application?

progapandist commented 6 years ago

There are no plans to support Rails yet, as this is the "bot-end" concept where bot is a separate REST microservice.

segun-adeleye commented 6 years ago

Nice. The separation makes sense.