Class: FlowChat::Whatsapp::TemplateManager
- Inherits:
-
Object
- Object
- FlowChat::Whatsapp::TemplateManager
- Defined in:
- lib/flow_chat/whatsapp/template_manager.rb
Instance Method Summary collapse
-
#create_template(name:, category:, language: "en_US", components: []) ⇒ Object
Create a new template (requires approval from Meta).
-
#initialize(config = nil) ⇒ TemplateManager
constructor
A new instance of TemplateManager.
-
#list_templates ⇒ Object
List all templates.
- #send_notification_template(to:, message:, button_text: nil) ⇒ Object
-
#send_template(to:, template_name:, language: "en_US", components: []) ⇒ Object
Send a template message (used to initiate conversations).
-
#send_welcome_template(to:, name: nil) ⇒ Object
Common template structures.
-
#template_status(template_id) ⇒ Object
Get template status.
Constructor Details
#initialize(config = nil) ⇒ TemplateManager
Returns a new instance of TemplateManager.
7 8 9 |
# File 'lib/flow_chat/whatsapp/template_manager.rb', line 7 def initialize(config = nil) @config = config || Configuration.from_credentials end |
Instance Method Details
#create_template(name:, category:, language: "en_US", components: []) ⇒ Object
Create a new template (requires approval from Meta)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/flow_chat/whatsapp/template_manager.rb', line 87 def create_template(name:, category:, language: "en_US", components: []) business_account_id = @config.business_account_id uri = URI("#{FlowChat::Config.whatsapp.api_base_url}/#{business_account_id}/message_templates") template_data = { name: name, category: category, # AUTHENTICATION, MARKETING, UTILITY language: language, components: components } http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Post.new(uri) request["Authorization"] = "Bearer #{@config.access_token}" request["Content-Type"] = "application/json" request.body = template_data.to_json response = http.request(request) JSON.parse(response.body) end |
#list_templates ⇒ Object
List all templates
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/flow_chat/whatsapp/template_manager.rb', line 111 def list_templates business_account_id = @config.business_account_id uri = URI("#{FlowChat::Config.whatsapp.api_base_url}/#{business_account_id}/message_templates") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Get.new(uri) request["Authorization"] = "Bearer #{@config.access_token}" response = http.request(request) JSON.parse(response.body) end |
#send_notification_template(to:, message:, button_text: nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/flow_chat/whatsapp/template_manager.rb', line 51 def send_notification_template(to:, message:, button_text: nil) components = [ { type: "body", parameters: [ { type: "text", text: } ] } ] if components << { type: "button", sub_type: "quick_reply", index: "0", parameters: [ { type: "payload", payload: "continue" } ] } end send_template( to: to, template_name: "notification_template", # Custom template language: "en_US", components: components ) end |
#send_template(to:, template_name:, language: "en_US", components: []) ⇒ Object
Send a template message (used to initiate conversations)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/flow_chat/whatsapp/template_manager.rb', line 12 def send_template(to:, template_name:, language: "en_US", components: []) = { messaging_product: "whatsapp", to: to, type: "template", template: { name: template_name, language: {code: language}, components: components } } () end |
#send_welcome_template(to:, name: nil) ⇒ Object
Common template structures
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/flow_chat/whatsapp/template_manager.rb', line 28 def send_welcome_template(to:, name: nil) components = [] if name components << { type: "header", parameters: [ { type: "text", text: name } ] } end send_template( to: to, template_name: "hello_world", # Default Meta template language: "en_US", components: components ) end |
#template_status(template_id) ⇒ Object
Get template status
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/flow_chat/whatsapp/template_manager.rb', line 126 def template_status(template_id) uri = URI("#{FlowChat::Config.whatsapp.api_base_url}/#{template_id}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Get.new(uri) request["Authorization"] = "Bearer #{@config.access_token}" response = http.request(request) JSON.parse(response.body) end |