flow_chat 0.2.1 → 0.4.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/.ruby-version +1 -0
- data/Gemfile +3 -1
- data/README.md +784 -63
- data/Rakefile +7 -3
- data/examples/initializer.rb +31 -0
- data/examples/multi_tenant_whatsapp_controller.rb +248 -0
- data/examples/ussd_controller.rb +264 -0
- data/examples/whatsapp_controller.rb +141 -0
- data/lib/flow_chat/base_processor.rb +63 -0
- data/lib/flow_chat/config.rb +21 -8
- data/lib/flow_chat/context.rb +8 -0
- data/lib/flow_chat/interrupt.rb +2 -0
- data/lib/flow_chat/session/cache_session_store.rb +84 -0
- data/lib/flow_chat/session/middleware.rb +15 -7
- data/lib/flow_chat/ussd/app.rb +25 -0
- data/lib/flow_chat/ussd/gateway/nalo.rb +3 -1
- data/lib/flow_chat/ussd/gateway/nsano.rb +7 -1
- data/lib/flow_chat/ussd/middleware/pagination.rb +14 -17
- data/lib/flow_chat/ussd/middleware/resumable_session.rb +18 -31
- data/lib/flow_chat/ussd/processor.rb +15 -42
- data/lib/flow_chat/ussd/prompt.rb +1 -1
- data/lib/flow_chat/ussd/simulator/controller.rb +1 -1
- data/lib/flow_chat/version.rb +1 -1
- data/lib/flow_chat/whatsapp/app.rb +58 -0
- data/lib/flow_chat/whatsapp/configuration.rb +75 -0
- data/lib/flow_chat/whatsapp/gateway/cloud_api.rb +213 -0
- data/lib/flow_chat/whatsapp/middleware/executor.rb +30 -0
- data/lib/flow_chat/whatsapp/processor.rb +36 -0
- data/lib/flow_chat/whatsapp/prompt.rb +206 -0
- data/lib/flow_chat/whatsapp/template_manager.rb +162 -0
- data/lib/flow_chat.rb +1 -0
- metadata +17 -4
- data/.rspec +0 -3
@@ -0,0 +1,162 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
module FlowChat
|
5
|
+
module Whatsapp
|
6
|
+
class TemplateManager
|
7
|
+
def initialize(config = nil)
|
8
|
+
@config = config || Configuration.from_credentials
|
9
|
+
end
|
10
|
+
|
11
|
+
# Send a template message (used to initiate conversations)
|
12
|
+
def send_template(to:, template_name:, language: "en_US", components: [])
|
13
|
+
message_data = {
|
14
|
+
messaging_product: "whatsapp",
|
15
|
+
to: to,
|
16
|
+
type: "template",
|
17
|
+
template: {
|
18
|
+
name: template_name,
|
19
|
+
language: { code: language },
|
20
|
+
components: components
|
21
|
+
}
|
22
|
+
}
|
23
|
+
|
24
|
+
send_message(message_data)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Common template structures
|
28
|
+
def send_welcome_template(to:, name: nil)
|
29
|
+
components = []
|
30
|
+
|
31
|
+
if name
|
32
|
+
components << {
|
33
|
+
type: "header",
|
34
|
+
parameters: [
|
35
|
+
{
|
36
|
+
type: "text",
|
37
|
+
text: name
|
38
|
+
}
|
39
|
+
]
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
send_template(
|
44
|
+
to: to,
|
45
|
+
template_name: "hello_world", # Default Meta template
|
46
|
+
language: "en_US",
|
47
|
+
components: components
|
48
|
+
)
|
49
|
+
end
|
50
|
+
|
51
|
+
def send_notification_template(to:, message:, button_text: nil)
|
52
|
+
components = [
|
53
|
+
{
|
54
|
+
type: "body",
|
55
|
+
parameters: [
|
56
|
+
{
|
57
|
+
type: "text",
|
58
|
+
text: message
|
59
|
+
}
|
60
|
+
]
|
61
|
+
}
|
62
|
+
]
|
63
|
+
|
64
|
+
if button_text
|
65
|
+
components << {
|
66
|
+
type: "button",
|
67
|
+
sub_type: "quick_reply",
|
68
|
+
index: "0",
|
69
|
+
parameters: [
|
70
|
+
{
|
71
|
+
type: "payload",
|
72
|
+
payload: "continue"
|
73
|
+
}
|
74
|
+
]
|
75
|
+
}
|
76
|
+
end
|
77
|
+
|
78
|
+
send_template(
|
79
|
+
to: to,
|
80
|
+
template_name: "notification_template", # Custom template
|
81
|
+
language: "en_US",
|
82
|
+
components: components
|
83
|
+
)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Create a new template (requires approval from Meta)
|
87
|
+
def create_template(name:, category:, language: "en_US", components: [])
|
88
|
+
business_account_id = @config.business_account_id
|
89
|
+
uri = URI("https://graph.facebook.com/v18.0/#{business_account_id}/message_templates")
|
90
|
+
|
91
|
+
template_data = {
|
92
|
+
name: name,
|
93
|
+
category: category, # AUTHENTICATION, MARKETING, UTILITY
|
94
|
+
language: language,
|
95
|
+
components: components
|
96
|
+
}
|
97
|
+
|
98
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
99
|
+
http.use_ssl = true
|
100
|
+
|
101
|
+
request = Net::HTTP::Post.new(uri)
|
102
|
+
request["Authorization"] = "Bearer #{@config.access_token}"
|
103
|
+
request["Content-Type"] = "application/json"
|
104
|
+
request.body = template_data.to_json
|
105
|
+
|
106
|
+
response = http.request(request)
|
107
|
+
JSON.parse(response.body)
|
108
|
+
end
|
109
|
+
|
110
|
+
# List all templates
|
111
|
+
def list_templates
|
112
|
+
business_account_id = @config.business_account_id
|
113
|
+
uri = URI("https://graph.facebook.com/v18.0/#{business_account_id}/message_templates")
|
114
|
+
|
115
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
116
|
+
http.use_ssl = true
|
117
|
+
|
118
|
+
request = Net::HTTP::Get.new(uri)
|
119
|
+
request["Authorization"] = "Bearer #{@config.access_token}"
|
120
|
+
|
121
|
+
response = http.request(request)
|
122
|
+
JSON.parse(response.body)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Get template status
|
126
|
+
def template_status(template_id)
|
127
|
+
uri = URI("https://graph.facebook.com/v18.0/#{template_id}")
|
128
|
+
|
129
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
130
|
+
http.use_ssl = true
|
131
|
+
|
132
|
+
request = Net::HTTP::Get.new(uri)
|
133
|
+
request["Authorization"] = "Bearer #{@config.access_token}"
|
134
|
+
|
135
|
+
response = http.request(request)
|
136
|
+
JSON.parse(response.body)
|
137
|
+
end
|
138
|
+
|
139
|
+
private
|
140
|
+
|
141
|
+
def send_message(message_data)
|
142
|
+
uri = URI(@config.messages_url)
|
143
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
144
|
+
http.use_ssl = true
|
145
|
+
|
146
|
+
request = Net::HTTP::Post.new(uri)
|
147
|
+
request["Authorization"] = "Bearer #{@config.access_token}"
|
148
|
+
request["Content-Type"] = "application/json"
|
149
|
+
request.body = message_data.to_json
|
150
|
+
|
151
|
+
response = http.request(request)
|
152
|
+
|
153
|
+
unless response.is_a?(Net::HTTPSuccess)
|
154
|
+
Rails.logger.error "WhatsApp Template API error: #{response.body}"
|
155
|
+
return nil
|
156
|
+
end
|
157
|
+
|
158
|
+
JSON.parse(response.body)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
data/lib/flow_chat.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flow_chat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Froelich
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: zeitwerk
|
@@ -89,7 +89,7 @@ extra_rdoc_files: []
|
|
89
89
|
files:
|
90
90
|
- ".DS_Store"
|
91
91
|
- ".gitignore"
|
92
|
-
- ".
|
92
|
+
- ".ruby-version"
|
93
93
|
- ".travis.yml"
|
94
94
|
- Gemfile
|
95
95
|
- LICENSE.txt
|
@@ -97,13 +97,19 @@ files:
|
|
97
97
|
- Rakefile
|
98
98
|
- bin/console
|
99
99
|
- bin/setup
|
100
|
+
- examples/initializer.rb
|
101
|
+
- examples/multi_tenant_whatsapp_controller.rb
|
102
|
+
- examples/ussd_controller.rb
|
103
|
+
- examples/whatsapp_controller.rb
|
100
104
|
- flow_chat.gemspec
|
101
105
|
- images/ussd_simulator.png
|
102
106
|
- lib/flow_chat.rb
|
107
|
+
- lib/flow_chat/base_processor.rb
|
103
108
|
- lib/flow_chat/config.rb
|
104
109
|
- lib/flow_chat/context.rb
|
105
110
|
- lib/flow_chat/flow.rb
|
106
111
|
- lib/flow_chat/interrupt.rb
|
112
|
+
- lib/flow_chat/session/cache_session_store.rb
|
107
113
|
- lib/flow_chat/session/middleware.rb
|
108
114
|
- lib/flow_chat/session/rails_session_store.rb
|
109
115
|
- lib/flow_chat/ussd/app.rb
|
@@ -118,6 +124,13 @@ files:
|
|
118
124
|
- lib/flow_chat/ussd/simulator/controller.rb
|
119
125
|
- lib/flow_chat/ussd/simulator/views/simulator.html.erb
|
120
126
|
- lib/flow_chat/version.rb
|
127
|
+
- lib/flow_chat/whatsapp/app.rb
|
128
|
+
- lib/flow_chat/whatsapp/configuration.rb
|
129
|
+
- lib/flow_chat/whatsapp/gateway/cloud_api.rb
|
130
|
+
- lib/flow_chat/whatsapp/middleware/executor.rb
|
131
|
+
- lib/flow_chat/whatsapp/processor.rb
|
132
|
+
- lib/flow_chat/whatsapp/prompt.rb
|
133
|
+
- lib/flow_chat/whatsapp/template_manager.rb
|
121
134
|
homepage: https://github.com/radioactive-labs/flow_chat
|
122
135
|
licenses:
|
123
136
|
- MIT
|
@@ -141,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
154
|
- !ruby/object:Gem::Version
|
142
155
|
version: '0'
|
143
156
|
requirements: []
|
144
|
-
rubygems_version: 3.
|
157
|
+
rubygems_version: 3.4.10
|
145
158
|
signing_key:
|
146
159
|
specification_version: 4
|
147
160
|
summary: Framework for building Menu based conversations (e.g. USSD) in Rails.
|
data/.rspec
DELETED