Files
moreminimore-chat/app/services/twilio/template_sync_service.rb
Muhsin Keloth 1c28df49e5 feat(whatsapp): expose cached Twilio templates (#15311)
The cached inbox template endpoint currently serves only native WhatsApp
channels. This draft extends the same read-only endpoint to Twilio
WhatsApp inboxes and adds last-sync metadata for both providers, so the
template listing can use one stable contract without making provider
requests.

Non-WhatsApp inbox behavior remains unchanged.

Related:
https://linear.app/chatwoot/issue/PLA-193/add-whatsapp-template-listing-to-account-settings

### Things to know

- This is stack 1 of 2 and contains only the API contract needed by the
settings UI in #15312.
- Template data remains cache-only; the endpoint does not call Meta or
Twilio.
- Native WhatsApp templates are filtered by `name`; Twilio Content
Templates are filtered by `friendly_name`.

### How to test

1. Request the message templates endpoint for a native WhatsApp inbox
and confirm it returns the cached templates plus `meta.last_updated_at`.
2. Request it for a Twilio WhatsApp inbox and confirm it returns cached
Content Templates plus `meta.last_updated_at`.
3. Pass a template name and confirm only the matching provider template
is returned.
4. Request it for a non-WhatsApp inbox and confirm the endpoint returns
an unprocessable entity response.

---------

Co-authored-by: Muhsin <12408980+muhsin-k@users.noreply.github.com>
2026-08-04 14:13:11 +04:00

116 lines
3.0 KiB
Ruby

class Twilio::TemplateSyncService
pattr_initialize [:channel!]
def call
mark_templates_updated
fetch_templates_from_twilio
update_channel_templates
rescue Twilio::REST::TwilioError => e
Rails.logger.error("Twilio template sync failed: #{e.message}")
false
end
private
def fetch_templates_from_twilio
@templates = client.content.v1.content_and_approvals.list(limit: 1000)
end
def update_channel_templates
formatted_templates = @templates.map { |template| format_template(template) }
channel.update!(content_templates: { templates: formatted_templates })
end
def format_template(template)
{
content_sid: template.sid,
friendly_name: template.friendly_name,
language: template.language,
status: derive_status(template),
template_type: derive_template_type(template),
media_type: derive_media_type(template),
variables: template.variables || {},
category: derive_category(template),
body: extract_body_content(template),
types: template.types,
created_at: template.date_created,
updated_at: template.date_updated
}
end
def mark_templates_updated
channel.update!(content_templates_last_updated: Time.current)
end
def client
@client ||= channel.send(:client)
end
def derive_status(template)
template.approval_requests&.dig('whatsapp', 'status')&.downcase || 'unsubmitted'
end
def derive_template_type(template)
template_types = template.types.keys
if template_types.include?('twilio/media')
'media'
elsif template_types.include?('twilio/quick-reply')
'quick_reply'
elsif template_types.include?('twilio/call-to-action')
'call_to_action'
elsif template_types.include?('twilio/catalog')
'catalog'
else
'text'
end
end
def derive_media_type(template)
return nil unless derive_template_type(template) == 'media'
media_content = template.types['twilio/media']
return nil unless media_content
if media_content['image']
'image'
elsif media_content['video']
'video'
elsif media_content['document']
'document'
end
end
def derive_category(template)
# Map template friendly names or other attributes to categories
# For now, use utility as default
case template.friendly_name
when /marketing|promo|offer|sale/i
'marketing'
when /auth|otp|verify|code/i
'authentication'
else
'utility'
end
end
def extract_body_content(template)
template_types = template.types
if template_types['twilio/text']
template_types['twilio/text']['body']
elsif template_types['twilio/media']
template_types['twilio/media']['body']
elsif template_types['twilio/quick-reply']
template_types['twilio/quick-reply']['body']
elsif template_types['twilio/call-to-action']
template_types['twilio/call-to-action']['body']
elsif template_types['twilio/catalog']
template_types['twilio/catalog']['body']
else
''
end
end
end