Sha256: c940e1cf2205a36209e89113e81019da73418d90e903a8e39165fc683fa704ae

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

module Messenger
  module Bot
    class Space::StationController < ::MessengerBotController
      before_action :authenticate, only: [:receive]

      def validation
        if params["hub.verify_token"] === Messenger::Bot::Config.validation_token
          return render json: params["hub.challenge"]
        end
        render body: "Error, wrong validation token"
      end

      def receive
        Messenger::Bot::Receiver.share(params)
        params["entry"].each do |entry|
          messaging_events = entry["messaging"]
          messaging_events.each_with_index do |event, key|
            sender = Messenger::Bot::Transmitter.new(event["sender"]["id"])
            if event["message"] && !defined?(message).nil?
              send(:message, event, sender)
            elsif event["postback"] && !defined?(postback).nil?
              send(:postback, event, sender)
            elsif event["delivery"] && !defined?(delivery).nil?
              send(:delivery, event, sender)
            end 
          end 
        end
        render body: "ok"
      end

      def authenticate
        return true if Messenger::Bot::Config.secret_token.nil?
        signature = "sha1=" + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), Messenger::Bot::Config.secret_token, request.body.read)
        return render(body: "Error, Signatures didn't match", status: 500) unless Rack::Utils.secure_compare(signature, request.env['HTTP_X_HUB_SIGNATURE'])
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
messenger-bot-1.0.2 lib/messenger/bot/station.rb
messenger-bot-1.0.0 lib/messenger/bot/station.rb