chat_sdk-twilio 1.0.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/chat_sdk/twilio/adapter.rb +40 -3
- data/lib/chat_sdk/twilio/api_client.rb +32 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d2dd3ae8329cad676f451bc464b2eafeef1ce820389c90d62e07188d0d1972b7
|
|
4
|
+
data.tar.gz: 6cf517080a7184e81c90a99c6f27a8c61a054212bf6e2051e8088c42b23eff35
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: aa5c5d88e8acaf27c337b714fbf86f7697e38419247cbac6ccb5371e5cbfcb9be75ae58c828f32972d91d8e5b139ebb8b68f22bf49f5080ae27f1c3209ab855c
|
|
7
|
+
data.tar.gz: a117515eafebd41117bdde2c37096ec91220c6e0aea0c677beabbf79702f3c92df6832b856305a100204cc86db488529a06b65ba225e09da4ce4b89f4e6b2e4f
|
|
@@ -5,16 +5,24 @@ require "rack/utils"
|
|
|
5
5
|
module ChatSDK
|
|
6
6
|
module Twilio
|
|
7
7
|
class Adapter < ChatSDK::Adapter::Base
|
|
8
|
-
capabilities :direct_messages, :message_history, :delete_messages
|
|
8
|
+
capabilities :direct_messages, :message_history, :delete_messages, :typing_indicator
|
|
9
9
|
|
|
10
10
|
attr_reader :client
|
|
11
11
|
|
|
12
|
-
def initialize(
|
|
12
|
+
def initialize(
|
|
13
|
+
account_sid: nil,
|
|
14
|
+
auth_token: nil,
|
|
15
|
+
phone_number: nil,
|
|
16
|
+
messaging_service_sid: nil,
|
|
17
|
+
webhook_url: nil,
|
|
18
|
+
messaging_api_url: nil
|
|
19
|
+
)
|
|
13
20
|
@account_sid = account_sid || ENV["TWILIO_ACCOUNT_SID"]
|
|
14
21
|
@auth_token = auth_token || ENV["TWILIO_AUTH_TOKEN"]
|
|
15
22
|
@phone_number = phone_number || ENV["TWILIO_PHONE_NUMBER"]
|
|
16
23
|
@messaging_service_sid = messaging_service_sid || ENV["TWILIO_MESSAGING_SERVICE_SID"]
|
|
17
24
|
@webhook_url = webhook_url
|
|
25
|
+
@messaging_api_url = messaging_api_url || ENV["TWILIO_MESSAGING_API_URL"] || "https://messaging.twilio.com"
|
|
18
26
|
|
|
19
27
|
raise ChatSDK::ConfigurationError, "Twilio account_sid required" unless @account_sid
|
|
20
28
|
raise ChatSDK::ConfigurationError, "Twilio auth_token required" unless @auth_token
|
|
@@ -72,6 +80,27 @@ module ChatSDK
|
|
|
72
80
|
parse_twilio_message(result, channel_id)
|
|
73
81
|
end
|
|
74
82
|
|
|
83
|
+
def send_template(channel_id:, content_sid:, content_variables: nil)
|
|
84
|
+
result = @client.send_template(
|
|
85
|
+
to: channel_id,
|
|
86
|
+
content_sid: content_sid,
|
|
87
|
+
content_variables: content_variables,
|
|
88
|
+
from: @phone_number,
|
|
89
|
+
messaging_service_sid: @messaging_service_sid
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
parse_twilio_message(result, channel_id)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def start_typing(channel_id:, thread_id: nil, message_id: nil) # rubocop:disable Lint/UnusedMethodArgument
|
|
96
|
+
return if message_id.to_s.empty?
|
|
97
|
+
|
|
98
|
+
body = typing_indicator_body(channel_id, message_id)
|
|
99
|
+
return unless body
|
|
100
|
+
|
|
101
|
+
@client.start_typing(body: body, messaging_api_url: @messaging_api_url)
|
|
102
|
+
end
|
|
103
|
+
|
|
75
104
|
def fetch_messages(channel_id:, thread_id: nil, limit: 20) # rubocop:disable Lint/UnusedMethodArgument
|
|
76
105
|
result = @client.list_messages(to: channel_id, limit: limit)
|
|
77
106
|
messages = (result["messages"] || []).map do |msg|
|
|
@@ -93,7 +122,7 @@ module ChatSDK
|
|
|
93
122
|
[messages, nil]
|
|
94
123
|
end
|
|
95
124
|
|
|
96
|
-
def delete_message(channel_id:, message_id
|
|
125
|
+
def delete_message(channel_id:, message_id:, **_options) # rubocop:disable Lint/UnusedMethodArgument
|
|
97
126
|
@client.delete_message(message_sid: message_id)
|
|
98
127
|
end
|
|
99
128
|
|
|
@@ -121,6 +150,14 @@ module ChatSDK
|
|
|
121
150
|
msg.text || msg.card&.fallback_text || ""
|
|
122
151
|
end
|
|
123
152
|
|
|
153
|
+
def typing_indicator_body(channel_id, message_id)
|
|
154
|
+
if channel_id.to_s.start_with?("whatsapp:")
|
|
155
|
+
{"channel" => "WHATSAPP", "messageId" => message_id}
|
|
156
|
+
elsif @phone_number.to_s.start_with?("rcs:") && channel_id.to_s.start_with?("rcs:")
|
|
157
|
+
{"channel" => "RCS", "event" => "START", "from" => @phone_number, "to" => channel_id}
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
124
161
|
def parse_twilio_message(data, channel_id)
|
|
125
162
|
ChatSDK::Message.new(
|
|
126
163
|
id: data["sid"],
|
|
@@ -20,6 +20,26 @@ module ChatSDK
|
|
|
20
20
|
handle_response(response)
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
+
def send_template(to:, content_sid:, content_variables: nil, from: nil, messaging_service_sid: nil)
|
|
24
|
+
params = {"To" => to, "ContentSid" => content_sid}
|
|
25
|
+
if content_variables
|
|
26
|
+
params["ContentVariables"] = content_variables.is_a?(String) ? content_variables : JSON.generate(content_variables)
|
|
27
|
+
end
|
|
28
|
+
params["From"] = from if from
|
|
29
|
+
params["MessagingServiceSid"] = messaging_service_sid if messaging_service_sid
|
|
30
|
+
|
|
31
|
+
response = connection.post(messages_path, URI.encode_www_form(params))
|
|
32
|
+
handle_response(response)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def start_typing(body:, messaging_api_url: "https://messaging.twilio.com")
|
|
36
|
+
response = messaging_connection(messaging_api_url).post(
|
|
37
|
+
"/v3/Indicators/Typing.json",
|
|
38
|
+
JSON.generate(body)
|
|
39
|
+
)
|
|
40
|
+
handle_response(response)
|
|
41
|
+
end
|
|
42
|
+
|
|
23
43
|
def list_messages(to: nil, from: nil, limit: 20)
|
|
24
44
|
params = {"PageSize" => limit.to_s}
|
|
25
45
|
params["To"] = to if to
|
|
@@ -50,6 +70,15 @@ module ChatSDK
|
|
|
50
70
|
end
|
|
51
71
|
end
|
|
52
72
|
|
|
73
|
+
def messaging_connection(url)
|
|
74
|
+
Faraday.new(url: url) do |f|
|
|
75
|
+
f.request :authorization, :basic, @account_sid, @auth_token
|
|
76
|
+
f.headers["Content-Type"] = "application/json"
|
|
77
|
+
f.response :json
|
|
78
|
+
f.adapter :net_http
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
53
82
|
def handle_response(response)
|
|
54
83
|
body = response.body
|
|
55
84
|
|
|
@@ -66,8 +95,10 @@ module ChatSDK
|
|
|
66
95
|
end
|
|
67
96
|
|
|
68
97
|
message = body.is_a?(Hash) ? body["message"] : response.status
|
|
98
|
+
code = body.is_a?(Hash) ? body["code"] : nil
|
|
99
|
+
error = code ? "Twilio API error (#{code}): #{message}" : "Twilio API error: #{message}"
|
|
69
100
|
raise ChatSDK::PlatformError.new(
|
|
70
|
-
|
|
101
|
+
error,
|
|
71
102
|
status: response.status,
|
|
72
103
|
body: body,
|
|
73
104
|
adapter_name: :twilio
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: chat_sdk-twilio
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0
|
|
4
|
+
version: 1.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Quentin Rousseau
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: '
|
|
18
|
+
version: '1.2'
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - ">="
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: '
|
|
25
|
+
version: '1.2'
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: faraday
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|