Files
leadchat/app/services/telegram/update_message_service.rb
Pranav 1b1ba3f8dd fix: Update the photo/video caption when an update event is received (#10804)
The update ensures proper handling of text updates in photo/video
messages by accounting for the caption attribute in addition to the text
attribute. This change enables consistent processing across both
messages.

Fixes https://github.com/chatwoot/chatwoot/issues/10760

Note: TIL, you can update the video/photo you’ve sent on Telegram, not
just the text. Currently, we’re not handling this. To support it, we
need to parse the payload and update the attachments accordingly. This
could be taken as a followup.
2025-02-03 12:44:10 -08:00

40 lines
1.1 KiB
Ruby

# Find the various telegram payload samples here: https://core.telegram.org/bots/webhooks#testing-your-bot-with-updates
# https://core.telegram.org/bots/api#available-types
class Telegram::UpdateMessageService
pattr_initialize [:inbox!, :params!]
def perform
find_contact_inbox
find_conversation
find_message
update_message
rescue StandardError => e
Rails.logger.error "Error while processing telegram message update #{e.message}"
end
private
def find_contact_inbox
@contact_inbox = inbox.contact_inboxes.find_by!(source_id: params[:edited_message][:chat][:id])
end
def find_conversation
@conversation = @contact_inbox.conversations.last
end
def find_message
@message = @conversation.messages.find_by(source_id: params[:edited_message][:message_id])
end
def update_message
edited_message = params[:edited_message]
if edited_message[:text].present?
@message.update!(content: edited_message[:text])
elsif edited_message[:caption].present?
@message.update!(content: edited_message[:caption])
end
end
end