Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nothing happens until a button is pressed multiple times #29

Open
Quintasan opened this issue Feb 10, 2019 · 6 comments
Open

Nothing happens until a button is pressed multiple times #29

Quintasan opened this issue Feb 10, 2019 · 6 comments

Comments

@Quintasan
Copy link

In my say_hello.yml.erb file I have the following reply

- reply_type: text
  text: "Nasz profil na Facebooku domyślnie obsługuje bot przyjmujący rezerwacje. Jeżeli nie uda mu się Ciebie obsłużyć to kontrolę przejmie ktoś z załogi Incognito :). Co chcesz zrobić?"
  buttons:
    - type: payload
      text: "Rezerwacja stolika."
      payload: "reservation"
    - type: payload
      text: "Mam inne pytanie."
      payload: "other"

The options do show up, clicking them sends the message but nothing happens in my bot for some reason.

screenshot_20190210_145625

The only thing I get in the Stealth server log is:

14:52:03 sidekiq.1 | [previous_session] User 1023058087818337: setting to hello->say_hello
14:52:03 sidekiq.1 | [session] User 1023058087818337: setting session to hello->get_selection
14:52:03 sidekiq.1 | 2019-02-10T13:52:03.867Z 21448 TID-grr2o3b8g Stealth::Services::HandleMessageJob JID-7810baeeb7d061e9850f1c8e INFO: done: 1.923 sec
14:52:04 web.1     | [incoming] Received webhook from facebook
14:52:04 web.1     | [incoming] Received webhook from facebook
14:52:09 web.1     | [incoming] Received webhook from facebook

Only after clicking it more than once (sometime it takes 2 time, sometimes I have to click on it over 9000 times) something does happen:

image

14:58:05 web.1     | [incoming] Received webhook from facebook
14:58:05 sidekiq.1 | 2019-02-10T13:58:05.413Z 21448 TID-grr2nrzqc Stealth::Services::HandleMessageJob JID-6aa3a86387ae9ae75a70056f INFO: start
14:58:05 sidekiq.1 | I, [2019-02-10T14:58:05.414264 #21448]  INFO -- : ---------------------------------------DUPA:
14:58:05 sidekiq.1 | [previous_session] User 1023058087818337: skipping setting to hello->get_selection because it is the same as current_session
14:58:05 sidekiq.1 | [session] User 1023058087818337: setting session to hello->get_selection

What am I doing wrong?

@Quintasan Quintasan changed the title Nothing happens when a button is pressed Nothing happens until a button is pressed multiple times Feb 10, 2019
@mgomes
Copy link
Member

mgomes commented Feb 10, 2019

I haven't seen this before, but payloads can be a little tricky because of some unexpected things. I can't say for sure until you paste your bot_controller.rb here, but here is what I do for Facebook:

class BotController < Stealth::Controller

  before_action :current_user

  def route
    if current_message.payload.present?
      handle_payloads
      current_message.payload = nil
      return
    end

    if current_session.present?
      step_to session: current_session
    else
      step_to flow: 'hello', state: 'say_hello'
    end
  end

  private

    def handle_payloads
      case current_message.payload
      when 'developer_restart', 'new_user'
        step_to flow: 'hello', state: 'say_hello'
      when 'something_else'
        step_to flow: 'some_other', state: 'state'
      end
    end

    def current_user
      @current_user ||= begin
        user = User.find_by(recipient_id: current_user_id)

        unless user.present?
          user = persist_current_user
        end

        user
      end
    end

    def persist_current_user
      fb_profile = Stealth::Services::Facebook::Client.fetch_profile(
        recipient_id: current_user_id
      )

      User.create!(
        recipient_id: current_user_id,
        first_name: fb_profile['first_name'],
        last_name: fb_profile['last_name'],
        profile_pic: fb_profile['profile_pic'],
      )
    end

end

The key is that payloads are handled globally like this before the user is even routed. So in the route method, I check if there is a payload and if so route accordingly. One important thing to not forget is to set currrent_message.payload to nil. This ensures whatever method you end up routing to doesn't have to deal with that same payload logic.

@Quintasan
Copy link
Author

It's rather plain

class BotController < Stealth::Controller
  include Logging

  helper :all

  def route
    logger.info "---------------------------------------DUPA: #{current_message.message}"
    if current_session.present?
      step_to session: current_session
    else
      step_to flow: 'hello', state: 'say_hello'
    end
  end

end

@Quintasan
Copy link
Author

I have updated to a similar version of what you have but I still had to click the button twice to get any reaction at all.

screenshot_20190210_232451

23:24:01 web.1     | [incoming] Received webhook from facebook
23:24:35 web.1     | [incoming] Received webhook from facebook
23:24:35 sidekiq.1 | 2019-02-10T22:24:35.871Z 26377 TID-gpuf4l1n5 Stealth::Services::HandleMessageJob JID-26af63a1cc4f6a87026ce9bc INFO: start
23:24:35 sidekiq.1 | I, [2019-02-10T23:24:35.872147 #26377]  INFO -- : ---------------------------------------DUPA:
23:24:35 sidekiq.1 | [previous_session] User 1023058087818337: skipping setting to hello->get_selection because it is the same as current_session
23:24:35 sidekiq.1 | [session] User 1023058087818337: setting session to hello->get_selection
23:24:35 sidekiq.1 | [previous_session] User 1023058087818337: setting to hello->get_selection
23:24:35 sidekiq.1 | [session] User 1023058087818337: setting session to reservation->ask_for_date
23:24:36 sidekiq.1 | [facebook] Transmitting. Response: 200: {"recipient_id":"1023058087818337","message_id":"m_J1RYWM26l8sXlDV-7oRM9TBWw9_3akcE0vMYl82HgIXe5he61iXZ7A3S2T5MTuC9vXR7_1NEB8s_wFZjIgYzWw"}

@Quintasan
Copy link
Author

@mgomes I can give you developer access to the facebook application if this would help you with anything since it's something I'm doing in my spare time

@mgomes
Copy link
Member

mgomes commented Feb 11, 2019

Can you confirm that for each time you press the a button you get a corresponding webhook from Facebook? Let's start there. I think something is up with how those are coming in.

@Quintasan
Copy link
Author

Quintasan commented Feb 11, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants