class Stealth::Services::Smooch::Client

Attributes

reply[R]

Public Class Methods

generate_jwt_token() click to toggle source
# File lib/stealth/services/smooch/client.rb, line 40
def self.generate_jwt_token
  payload = { scope: 'app' }
  jwtHeader = { kid: Stealth.config.smooch.key_id }
  token = JWT.encode(payload, Stealth.config.smooch.secret, 'HS256', jwtHeader)

  puts "#{Stealth::Logger.colorize('[JWT Token]', color: :green)} Your Smooch token is below. Please set the value `jwt_token` to the token in services.yml."
  puts token
end
new(reply:) click to toggle source
# File lib/stealth/services/smooch/client.rb, line 16
def initialize(reply:)
  @reply = reply
  @smooch = SmoochApi::ConversationApi.new
end
register_webhooks(endpoint:) click to toggle source
# File lib/stealth/services/smooch/client.rb, line 49
def self.register_webhooks(endpoint:)
  smooch_webhook_api = SmoochApi::WebhookApi.new
  webhook_create_body = SmoochApi::WebhookCreate.new(
    target: endpoint,
    triggers: ['message:appUser', 'postback']
  )

  response = smooch_webhook_api.create_webhook(
    Stealth.config.smooch.app_id,
    webhook_create_body
  )

  puts "#{Stealth::Logger.colorize('[Web Hooks]', color: :green)} Your Smooch webhooks have been registered to: #{endpoint}"
end
set_persistent_menu(menu) click to toggle source
# File lib/stealth/services/smooch/client.rb, line 64
def self.set_persistent_menu(menu)
  smooch_api = SmoochApi::IntegrationApi.new
  response = smooch_api.list_integrations(Stealth.config.smooch.app_id)
  response.integrations.each do |integration|
    begin
      smooch_api.update_integration_menu(Stealth.config.smooch.app_id, integration.id, menu)
      puts "#{Stealth::Logger.colorize('[Persistent Menu]', color: :green)} set for #{integration.type} integration."
    rescue SmoochApi::ApiError
      # Not all integrations support the persistent menu
      puts "#{Stealth::Logger.colorize('[Persistent Menu]', color: :red)} Skipping #{integration.type} integration. Persistent Menu is not supported."
      next
    end
  end
end

Public Instance Methods

transmit() click to toggle source
# File lib/stealth/services/smooch/client.rb, line 21
def transmit
  begin
    response = @smooch.send(
      reply[:reply_type],
      Stealth.config.smooch.app_id,
      reply[:recipient_id],
      reply[:message]
    )
  rescue SmoochApi::ApiError => e
    msg = Stealth::Logger.colorize('[Error]', color: :red) + " #{e.code}: #{e.response_body}"
    Stealth::Logger.l(topic: 'smooch', message: msg)
    raise Stealth::Errors::ServiceError
  end

  if response.present?
    Stealth::Logger.l(topic: "smooch", message: "Message #{response.message.id} successfully sent.")
  end
end