Class: ChatSDK::Twilio::Adapter

Inherits:
Adapter::Base
  • Object
show all
Defined in:
lib/chat_sdk/twilio/adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_sid: nil, auth_token: nil, phone_number: nil, messaging_service_sid: nil, webhook_url: nil) ⇒ Adapter

Returns a new instance of Adapter.

Raises:

  • (ChatSDK::ConfigurationError)


12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/chat_sdk/twilio/adapter.rb', line 12

def initialize(account_sid: nil, auth_token: nil, phone_number: nil, messaging_service_sid: nil, webhook_url: nil)
  @account_sid =  || ENV["TWILIO_ACCOUNT_SID"]
  @auth_token = auth_token || ENV["TWILIO_AUTH_TOKEN"]
  @phone_number = phone_number || ENV["TWILIO_PHONE_NUMBER"]
  @messaging_service_sid = messaging_service_sid || ENV["TWILIO_MESSAGING_SERVICE_SID"]
  @webhook_url = webhook_url

  raise ChatSDK::ConfigurationError, "Twilio account_sid required" unless @account_sid
  raise ChatSDK::ConfigurationError, "Twilio auth_token required" unless @auth_token
  raise ChatSDK::ConfigurationError, "Twilio phone_number or messaging_service_sid required" unless @phone_number || @messaging_service_sid

  @client = ApiClient.new(@account_sid, @auth_token)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/chat_sdk/twilio/adapter.rb', line 10

def client
  @client
end

Instance Method Details

#ack_response(_rack_request) ⇒ Object



47
48
49
# File 'lib/chat_sdk/twilio/adapter.rb', line 47

def ack_response(_rack_request)
  nil
end

#mention(user_id) ⇒ Object



87
88
89
# File 'lib/chat_sdk/twilio/adapter.rb', line 87

def mention(user_id)
  user_id
end

#nameObject



26
27
28
# File 'lib/chat_sdk/twilio/adapter.rb', line 26

def name
  :twilio
end

#open_dm(user_id) ⇒ Object



83
84
85
# File 'lib/chat_sdk/twilio/adapter.rb', line 83

def open_dm(user_id)
  user_id
end

#parse_events(rack_request) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/chat_sdk/twilio/adapter.rb', line 51

def parse_events(rack_request)
  body = rack_request.body.read
  rack_request.body.rewind

  params = Rack::Utils.parse_query(body)
  EventParser.parse(params)
rescue ArgumentError
  []
end

#post_message(channel_id:, message:, thread_id: nil) ⇒ Object

Outbound



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chat_sdk/twilio/adapter.rb', line 62

def post_message(channel_id:, message:, thread_id: nil) # rubocop:disable Lint/UnusedMethodArgument
  text = prepare_message_payload(message)

  result = @client.send_message(
    to: channel_id,
    body: text,
    from: @phone_number,
    messaging_service_sid: @messaging_service_sid
  )

  parse_twilio_message(result, channel_id)
end

#render(postable_message) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/chat_sdk/twilio/adapter.rb', line 91

def render(postable_message)
  if postable_message.card?
    @text_renderer ||= Cards::Renderer.new
    @text_renderer.render(postable_message.card)
  else
    postable_message.text
  end
end

#upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument

Raises:

  • (ChatSDK::PlatformError)


75
76
77
78
79
80
81
# File 'lib/chat_sdk/twilio/adapter.rb', line 75

def upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) # rubocop:disable Lint/UnusedMethodArgument
  raise ChatSDK::PlatformError.new(
    "Twilio MMS requires a publicly accessible MediaUrl. Binary upload is not supported. " \
    "Host the file at a public URL and send it as a message with the URL included.",
    adapter_name: :twilio
  )
end

#verify_request!(rack_request) ⇒ Object

Inbound



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chat_sdk/twilio/adapter.rb', line 31

def verify_request!(rack_request)
  signature = rack_request.get_header("HTTP_X_TWILIO_SIGNATURE")

  unless signature
    raise ChatSDK::SignatureVerificationError, "Missing Twilio signature header"
  end

  body = rack_request.body.read
  rack_request.body.rewind

  params = Rack::Utils.parse_query(body)
  url = @webhook_url || rack_request.url

  Signature.verify!(@auth_token, url, params, signature)
end