title: Messenger bot using Rails: setup for multiple pages
body: <p>I want to create a Messenger Bot used by different users for their Facebook pages. I created a rails app and use the <a href="https://github.com/jgorset/facebook-messenger" rel="nofollow noreferrer">facebook-messenger gem</a>.</p>
  
  <p>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 <a href="https://github.com/jgorset/facebook-messenger#make-a-configuration-provider" rel="nofollow noreferrer">"Make a configuration provider" section</a>).</p>
  
  <p>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:</p>
  
  <pre><code>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 &lt; 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 &lt; 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
  </code></pre>
  
  <p>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?</p>
  
  <pre><code>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'])
  </code></pre>
  
  <p>I searched in the Rails documentation how to make it work but unfortunately could not find anything.</p>
  
  <p>UPDATE:</p>
  
  <p>Now I'm able to set up the bots for different pages. Unfortunately, the following line of my ConfigProvider is getting an error:</p>
  
  <p>config/initializers/config_provider.rb</p>
  
  <pre><code>def bot
      Rails.application.class.parent::Bot
    end
  I'm getting the following error:
  </code></pre>
  
  <blockquote>
    <p>NameError (uninitialized constant BotletterApp::Bot):</p>
    
    <p>config/initializers/config_provider.rb:17:in bot'
    config/initializers/config_provider.rb:7:inapp_secret_for'</p>
  </blockquote>
  
  <p>Do you know how should I name my app?</p>
  
  <p>My BotModule:</p>
  
  <pre><code>module BotletterApp
    class Application &lt; 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', 'listen'), glob: File.join('**', '*.rb')
      config.autoload_paths += Dir[Rails.root.join('app', 'listen', '*')]
    end
  end
  </code></pre>
  
  <p>UPDATE, it works with ::Application, here is the new file:</p>
  
  <pre><code>class ConfigProvider &lt; Facebook::Messenger::Configuration::Providers::Base
    def valid_verify_token?(verify_token)
      ENV['VERIFY_TOKEN']
    end
  
    def app_secret_for(page_id)
      ENV['APP_SECRET']
    end
  
    def access_token_for(page_id)
      CoreBot.find_by_page_id(page_id).page_access_token
    end
  
    private
  
    def bot
      BotletterApp::Application
    end
  end
  
  Facebook::Messenger.configure do |config|
    config.provider = ConfigProvider.new
  end
  </code></pre>
  
  <p>The problem is I get the following error unless my db query seems working (it works in the rails console):</p>
  
  <blockquote>
    <p>ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column:
    page_id.id: SELECT  "core_bots".* FROM "core_bots" WHERE
    "page_id"."id" = ? LIMIT ?):</p>
  </blockquote>