flow_chat 0.3.0 → 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.
@@ -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
@@ -1,5 +1,6 @@
1
1
  require "zeitwerk"
2
2
  require "active_support"
3
+ require "active_support/core_ext/time"
3
4
 
4
5
  loader = Zeitwerk::Loader.for_gem
5
6
  loader.enable_reloading if defined?(Rails.env) && Rails.env.development?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flow_chat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Froelich
@@ -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