ruby-on-rails – 使用Rails的Messenger bot:设置多个页面

我想创建一个由不同用户用于其Facebook页面的Messenger Bot.我创建了一个rails应用程序并使用 facebook-messenger gem.

我成功创建了机器人,当我为一个页面设置时它可以工作.现在,我按照说明将我的机器人生活在多个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 ?):

解决方法

转向答案以提高可读性;)

关于’普通’……而不是

def bot
  Botletterapp::Application
end

使用

def bot
  Bot
end

或者(看起来你命名你的模型包含所有页面CoreBot(?)(假设你在/app/models/core_bot.rb中有一个典型的ActiveRecord模型,我假设是Bot)

def bot
  CoreBot
end

然后,您应该能够使用README.md中的模板代码

至于你的最新问题:似乎用一个哈希调用access_token_for-method,用{id:1}之类的东西进行搜索.您可能想要检查该值的来源.我建议稍微退一步,并更接近模板代码.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...