chore: Support telegram edited_message events (#6785)

- Add support for telegram edited_message events: Update the user message back in the Chatwoot dashboard when updated by the contact on telegram
This commit is contained in:
Sojan Jose
2023-03-29 18:48:16 +05:30
committed by GitHub
parent d1b65b6c9e
commit a4c9a2b2f2
5 changed files with 112 additions and 3 deletions

View File

@@ -7,6 +7,18 @@ class Webhooks::TelegramEventsJob < ApplicationJob
channel = Channel::Telegram.find_by(bot_token: params[:bot_token])
return unless channel
Telegram::IncomingMessageService.new(inbox: channel.inbox, params: params['telegram'].with_indifferent_access).perform
process_event_params(channel, params)
end
private
def process_event_params(channel, params)
return unless params[:telegram]
if params.dig(:telegram, :edited_message).present?
Telegram::UpdateMessageService.new(inbox: channel.inbox, params: params['telegram'].with_indifferent_access).perform
else
Telegram::IncomingMessageService.new(inbox: channel.inbox, params: params['telegram'].with_indifferent_access).perform
end
end
end

View File

@@ -0,0 +1,33 @@
# 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
@message.update!(content: params[:edited_message][:text])
end
end