coordinator-bot icon indicating copy to clipboard operation
coordinator-bot copied to clipboard

Bot only serves one person at a time

Open segun-adeleye opened this issue 7 years ago • 13 comments

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

segun-adeleye avatar May 09 '18 05:05 segun-adeleye

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 [email protected] 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

progapandist avatar May 09 '18 05:05 progapandist

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

segun-adeleye avatar May 09 '18 19:05 segun-adeleye

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 [email protected] 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

progapandist avatar May 09 '18 19:05 progapandist

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

segun-adeleye avatar May 09 '18 19:05 segun-adeleye

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)

progapandist avatar May 09 '18 20:05 progapandist

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

segun-adeleye avatar May 09 '18 21:05 segun-adeleye

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.

progapandist avatar May 10 '18 05:05 progapandist

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

segun-adeleye avatar May 12 '18 16:05 segun-adeleye

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

progapandist avatar May 12 '18 19:05 progapandist

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

progapandist avatar May 12 '18 19:05 progapandist

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

segun-adeleye avatar May 18 '18 14:05 segun-adeleye

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

progapandist avatar May 18 '18 14:05 progapandist

Nice. The separation makes sense.

segun-adeleye avatar May 25 '18 03:05 segun-adeleye