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.
This commit is contained in:
@@ -28,6 +28,12 @@ class Telegram::UpdateMessageService
|
||||
end
|
||||
|
||||
def update_message
|
||||
@message.update!(content: params[:edited_message][:text])
|
||||
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
|
||||
|
||||
@@ -2,41 +2,57 @@ require 'rails_helper'
|
||||
|
||||
describe Telegram::UpdateMessageService do
|
||||
let!(:telegram_channel) { create(:channel_telegram) }
|
||||
let!(:update_params) do
|
||||
let(:common_message_params) do
|
||||
{
|
||||
'update_id': 2_323_484,
|
||||
'edited_message': {
|
||||
'from': {
|
||||
'id': 123,
|
||||
'username': 'sojan'
|
||||
},
|
||||
'chat': {
|
||||
'id': 789,
|
||||
'type': 'private'
|
||||
},
|
||||
'date': Time.now.to_i,
|
||||
'edit_date': Time.now.to_i
|
||||
}
|
||||
end
|
||||
|
||||
let(:text_update_params) do
|
||||
{
|
||||
'update_id': 1,
|
||||
'edited_message': common_message_params.merge(
|
||||
'message_id': 48,
|
||||
'from': {
|
||||
'id': 512_313_123_171_248,
|
||||
'is_bot': false,
|
||||
'first_name': 'Sojan',
|
||||
'last_name': 'Jose',
|
||||
'username': 'sojan'
|
||||
},
|
||||
'chat': {
|
||||
'id': 517_123_213_211_248,
|
||||
'first_name': 'Sojan',
|
||||
'last_name': 'Jose',
|
||||
'username': 'sojan',
|
||||
'type': 'private'
|
||||
},
|
||||
'date': 1_680_088_034,
|
||||
'edit_date': 1_680_088_056,
|
||||
'text': 'updated message'
|
||||
}
|
||||
)
|
||||
}
|
||||
end
|
||||
|
||||
let(:caption_update_params) do
|
||||
{
|
||||
'update_id': 2,
|
||||
'edited_message': common_message_params.merge(
|
||||
'message_id': 49,
|
||||
'caption': 'updated caption'
|
||||
)
|
||||
}
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
context 'when valid update message params' do
|
||||
it 'updates the appropriate message' do
|
||||
contact_inbox = create(:contact_inbox, inbox: telegram_channel.inbox, source_id: update_params[:edited_message][:chat][:id])
|
||||
conversation = create(:conversation, contact_inbox: contact_inbox)
|
||||
message = create(:message, conversation: conversation, source_id: update_params[:edited_message][:message_id])
|
||||
described_class.new(inbox: telegram_channel.inbox, params: update_params.with_indifferent_access).perform
|
||||
let(:contact_inbox) { create(:contact_inbox, inbox: telegram_channel.inbox, source_id: common_message_params[:chat][:id]) }
|
||||
let(:conversation) { create(:conversation, contact_inbox: contact_inbox) }
|
||||
|
||||
it 'updates the message text when text is present' do
|
||||
message = create(:message, conversation: conversation, source_id: text_update_params[:edited_message][:message_id])
|
||||
described_class.new(inbox: telegram_channel.inbox, params: text_update_params.with_indifferent_access).perform
|
||||
expect(message.reload.content).to eq('updated message')
|
||||
end
|
||||
|
||||
it 'updates the message caption when caption is present' do
|
||||
message = create(:message, conversation: conversation, source_id: caption_update_params[:edited_message][:message_id])
|
||||
described_class.new(inbox: telegram_channel.inbox, params: caption_update_params.with_indifferent_access).perform
|
||||
expect(message.reload.content).to eq('updated caption')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when invalid update message params' do
|
||||
|
||||
Reference in New Issue
Block a user