我成功创建了机器人,当我为一个页面设置时它可以工作.现在,我按照说明将我的机器人生活在多个Facebook页面上(见“Make a configuration provider” section).
我是rails的新手,我不确定将ExampleProvider类放在哪里?我把它放在我的config / application.rb文件中:
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
然后我有我的app / bot / setup.rb文件来创建机器人.我不知道如何使用我创建的提供程序代替ENV变量?
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'])
我在Rails文档中搜索了如何使其工作,但遗憾的是找不到任何东西.
更新:
现在我可以为不同的页面设置机器人.不幸的是,我的ConfigProvider的以下行收到错误:
配置/初始化/ config_provider.rb
def bot Rails.application.class.parent::Bot end I'm getting the following error:
NameError (uninitialized constant BotletterApp::Bot):
config/initializers/config_provider.rb:17:in bot’
config/initializers/config_provider.rb:7:inapp_secret_for’
你知道我应该怎么命名我的应用程序吗?
我的BotModule:
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','listen'),'listen','*')] end end
UPDATE,它适用于:: Application,这是新文件:
class ConfigProvider < 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
问题是我得到以下错误,除非我的数据库查询似乎工作(它在rails控制台中工作):
ActiveRecord::StatementInvalid (sqlite3::sqlException: no such column:
page_id.id: SELECT “core_bots”.* FROM “core_bots” WHERE
“page_id”.”id” = ? LIMIT ?):