vendredi 2 juin 2017

Messenger bot using Rails: set up for multiple pages

I want to create a Messenger Bot used by different users for their Facebook pages. I created a rails app and use the facebook-messenger gem.

I successfully created the bot and it works when I do the set up for one page. Now, I follow the instructions to make my bot live on multiple Facebook Pages (see "Make a configuration provider" section).

I'm new to rails and I'm not sure where to put the class ExampleProvider? I put it in my config/application.rb file:

require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module BotletterApp
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
    config.paths.add File.join('app', 'bot'), glob: File.join('**', '*.rb')
    config.autoload_paths += Dir[Rails.root.join('app', 'bot', '*')]
  end

  class BotProvider < Facebook::Messenger::Configuration::Providers::Base
    def valid_verify_token?(verify_token)
      bot.exists?(verify_token: verify_token)
    end

    def app_secret_for()
      ENV['APP_SECRET']
    end

    def access_token_for(page_id)
      bot.find_by(user_id: page_id).page_access_token
    end

    private

    def bot
      MyApp::Bot
    end
  end

  Facebook::Messenger.configure do |config|
    config.provider = BotProvider.new
  end
end

Then I have my app/bot/setup.rb file to create the bot. I don't know how to use the provider I created in place of the ENV variables?

require "facebook/messenger"

include Facebook::Messenger

Facebook::Messenger::Subscriptions.subscribe(access_token: ENV["ACCESS_TOKEN"])

Facebook::Messenger::Profile.set({
  get_started: {
    payload: 'GET_STARTED_PAYLOAD'
  }
}, access_token: ENV['ACCESS_TOKEN'])

I searched on the Rails documentation how to make it work but unfortunately could not find anything.

Aucun commentaire:

Enregistrer un commentaire