Last active
May 20, 2016 23:30
-
-
Save phildionne/717de03bad079bc87888d67722f76c06 to your computer and use it in GitHub Desktop.
Dialog Analytics example integration in a Facebook Messenger and Ruby chatbot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative './dialog' | |
require 'facebook/messenger' # https://github.com/hyperoslo/facebook-messenger | |
Facebook::Messenger.configure do |config| | |
config.access_token = ENV['FACEBOOK_ACCESS_TOKEN'] | |
config.verify_token = ENV['FACEBOOK_SECRET_TOKEN'] | |
end | |
include Facebook::Messenger | |
Bot.on :message do |message| | |
Dialog.track(message) | |
text = 'Hello, human!' | |
message_id = Bot.deliver(recipient: message.sender, message: { text: text }) | |
params = { | |
to: message.sender, | |
sent_at: Time.now, | |
distinct_id: message_id, | |
properties: { | |
text: text | |
} | |
} | |
Dialog.track(params) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'http' # https://github.com/httprb/http | |
module Dialog | |
# @param message [Facebook::Messenger::Incoming::Message, Hash] | |
def track(message) | |
payload = if message.is_a?(Hash) | |
# Outbound | |
{ | |
message: { | |
platform: 'messenger', | |
type: 'text', | |
to: message[:to], | |
sent_at: message[:sent_at], | |
distinct_id: message[:id], | |
properties: { | |
text: message[:properties][:text] | |
} | |
} | |
} | |
# Inbound | |
# @note Only track text messages for the moment | |
elsif message.text | |
{ | |
message: { | |
platform: 'messenger', | |
type: 'text', | |
to: message.sender['id'], | |
sent_at: message.sent_at, | |
distinct_id: message.id, | |
properties: { | |
text: message.text | |
} | |
} | |
} | |
end | |
HTTP.post("https://api.dialoganalytics.com/v1/messages?token=#{ENV['DIALOG_TOKEN']}", json: payload) | |
end | |
module_function :track | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment