Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Facebook Messenger-节点到Ruby on Rails-示例_Ruby On Rails_Facebook_Facebook Messenger - Fatal编程技术网

Ruby on rails Facebook Messenger-节点到Ruby on Rails-示例

Ruby on rails Facebook Messenger-节点到Ruby on Rails-示例,ruby-on-rails,facebook,facebook-messenger,Ruby On Rails,Facebook,Facebook Messenger,Rails的新特性 在开始使用Facebook Messenger时,节点中有一个简单的脚本,可用于将应用程序中的消息发送到Messenger。有人能帮助我们如何在RubyonRails中实现它吗 我想在我的控制器中创建一个可以发送消息的方法。下面是Facebook的代码,我不知道如何在Rails中实现 var token = "<page_access_token>"; function sendTextMessage(sender, text) { messageData

Rails的新特性

在开始使用Facebook Messenger时,节点中有一个简单的脚本,可用于将应用程序中的消息发送到Messenger。有人能帮助我们如何在RubyonRails中实现它吗

我想在我的控制器中创建一个可以发送消息的方法。下面是Facebook的代码,我不知道如何在Rails中实现

var token = "<page_access_token>";

function sendTextMessage(sender, text) {
  messageData = {
    text:text
  }
  request({
    url: 'https://graph.facebook.com/v2.6/me/messages',
    qs: {access_token:token},
    method: 'POST',
    json: {
      recipient: {id:sender},
      message: messageData,
    }
  }, function(error, response, body) {
    if (error) {
      console.log('Error sending message: ', error);
    } else if (response.body.error) {
      console.log('Error: ', response.body.error);
    }
  });
}
这是我的控制器来接收信息

def apply_message
        @message = params["entry"][0]["messaging"][0]["message"]["text"]
        @sender_id = params["entry"][0]["messaging"][0]["sender"]["id"]
        if User.where(:fb_id => @sender_id.to_s ).present? #if sender exists
        # Send him a message saying "Hello World" Just like below
        else
                uri = URI.parse("https://graph.facebook.com/v2.6/me/messages?access_token=tocken_goes_here")
                http = Net::HTTP.new(uri.host, uri.port)
                json_data={
                    "recipient":{
                        "id":"@sender_id"
                    }, 
                    "message":{
                        "text":"Do you have an account with us?"
                    }
                }
                http.use_ssl = true
                http.verify_mode = OpenSSL::SSL::VERIFY_NONE
                req = Net::HTTP::Post.new(uri.request_uri)
                req.set_form_data(json_data)
                response = http.request(req)

        end

    end
创建一个FB模型:

class FacebookBot

  def send_message(data)
    url = URI.parse("https://graph.facebook.com/v2.6/me/messages?access_token=Token_here")

    http = Net::HTTP.new(url.host, 443)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE #only for development.
    begin
      request = Net::HTTP::Post.new(url.request_uri)
      request["Content-Type"] = "application/json"
      request.body = data.to_json
      response = http.request(request)
      body = JSON(response.body)
      return { ret: body["error"].nil?, body: body }
    rescue => e
      raise e
    end
  end

  def send_text_message(sender, text)
    data = {
      recipient: { id: sender },
      message: { text: text }
    }
    send_message(data)
  end

  def send_generic_message(sender, mes)
    data = {
      recipient: { id: sender },
      message: mes
    }
    send_message(data)
  end
end
现在我可以用这个来发送信息:

res = FacebookBot.new.send_text_message(sender, "Hi thanks for messaging")
享受。:)

创建一个FB模型:

class FacebookBot

  def send_message(data)
    url = URI.parse("https://graph.facebook.com/v2.6/me/messages?access_token=Token_here")

    http = Net::HTTP.new(url.host, 443)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE #only for development.
    begin
      request = Net::HTTP::Post.new(url.request_uri)
      request["Content-Type"] = "application/json"
      request.body = data.to_json
      response = http.request(request)
      body = JSON(response.body)
      return { ret: body["error"].nil?, body: body }
    rescue => e
      raise e
    end
  end

  def send_text_message(sender, text)
    data = {
      recipient: { id: sender },
      message: { text: text }
    }
    send_message(data)
  end

  def send_generic_message(sender, mes)
    data = {
      recipient: { id: sender },
      message: mes
    }
    send_message(data)
  end
end
现在我可以用这个来发送信息:

res = FacebookBot.new.send_text_message(sender, "Hi thanks for messaging")
享受。:)