Skip to content

Instantly share code, notes, and snippets.

@phildionne
Last active May 20, 2016 20:25
Show Gist options
  • Save phildionne/d5793b7ef80f1fd0f639e73245f37e5e to your computer and use it in GitHub Desktop.
Save phildionne/d5793b7ef80f1fd0f639e73245f37e5e to your computer and use it in GitHub Desktop.
Dialog Analytics example integration in a Telegram and Ruby chatbot.
require_relative './dialog'
require 'telegram/bot' # https://github.com/atipugin/telegram-bot-ruby
Telegram::Bot::Client.run(ENV.fetch('TELEGRAM_TOKEN')) do |bot|
bot.listen do |message|
case message.text
when '/test'
Dialog.track(message)
text = "Welcome to test"
response = bot.api.send_message(chat_id: message.chat.id, text: text)
Dialog.track(response)
end
end
end
module Dialog
# @param message [Telegram::Bot::Types::Message, Hash]
def track(message)
# Outbound
payload = if message.is_a?(Hash)
{
message: {
platform: 'telegram',
to: message['result']['chat']['id'],
sent_at: message['result']['date'],
distinct_id: message['result']['message_id'],
properties: {
text: message['result']['text']
}
}
}
# Inbound message
else
{
message: {
platform: 'telegram',
from: message.from.id,
sent_at: message.date,
distinct_id: message.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