feat: Telegram Channel (#2901)
- Ability to configure telegram bots as a channel in chatwoot - Receive a message sent to the telegram bot in chatwoot - Ability to reply to telegram users from chatwoot - Receive attachment messages in chatwoot fixes: #1843
This commit is contained in:
106
app/services/telegram/incoming_message_service.rb
Normal file
106
app/services/telegram/incoming_message_service.rb
Normal file
@@ -0,0 +1,106 @@
|
||||
class Telegram::IncomingMessageService
|
||||
include ::FileTypeHelper
|
||||
pattr_initialize [:inbox!, :params!]
|
||||
|
||||
def perform
|
||||
set_contact
|
||||
update_contact_avatar
|
||||
set_conversation
|
||||
@message = @conversation.messages.create(
|
||||
content: params[:message][:text],
|
||||
account_id: @inbox.account_id,
|
||||
inbox_id: @inbox.id,
|
||||
message_type: :incoming,
|
||||
sender: @contact,
|
||||
source_id: (params[:message][:message_id]).to_s
|
||||
)
|
||||
attach_files
|
||||
@message.save!
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def account
|
||||
@account ||= inbox.account
|
||||
end
|
||||
|
||||
def set_contact
|
||||
contact_inbox = ::ContactBuilder.new(
|
||||
source_id: params[:message][:from][:id],
|
||||
inbox: inbox,
|
||||
contact_attributes: contact_attributes
|
||||
).perform
|
||||
|
||||
@contact_inbox = contact_inbox
|
||||
@contact = contact_inbox.contact
|
||||
end
|
||||
|
||||
def update_contact_avatar
|
||||
return if @contact.avatar.attached?
|
||||
|
||||
avatar_url = inbox.channel.get_telegram_profile_image(params[:message][:from][:id])
|
||||
::ContactAvatarJob.perform_later(@contact, avatar_url) if avatar_url
|
||||
end
|
||||
|
||||
def conversation_params
|
||||
{
|
||||
account_id: @inbox.account_id,
|
||||
inbox_id: @inbox.id,
|
||||
contact_id: @contact.id,
|
||||
contact_inbox_id: @contact_inbox.id,
|
||||
additional_attributes: conversation_additional_attributes
|
||||
}
|
||||
end
|
||||
|
||||
def set_conversation
|
||||
@conversation = @contact_inbox.conversations.first
|
||||
return if @conversation
|
||||
|
||||
@conversation = ::Conversation.create!(conversation_params)
|
||||
end
|
||||
|
||||
def contact_attributes
|
||||
{
|
||||
name: "#{params[:message][:from][:first_name]} #{params[:message][:from][:last_name]}",
|
||||
additional_attributes: additional_attributes
|
||||
}
|
||||
end
|
||||
|
||||
def additional_attributes
|
||||
{
|
||||
username: params[:message][:from][:username],
|
||||
language_code: params[:message][:from][:language_code]
|
||||
}
|
||||
end
|
||||
|
||||
def conversation_additional_attributes
|
||||
{
|
||||
chat_id: params[:message][:chat][:id]
|
||||
}
|
||||
end
|
||||
|
||||
def file_content_type
|
||||
params[:message][:photo].present? ? :image : file_type(params[:message][:document][:mime_type])
|
||||
end
|
||||
|
||||
def attach_files
|
||||
file = params[:message][:document]
|
||||
file ||= params[:message][:photo]&.last
|
||||
|
||||
return unless file
|
||||
|
||||
attachment_file = Down.download(
|
||||
inbox.channel.get_telegram_file_path(file[:file_id])
|
||||
)
|
||||
|
||||
@message.attachments.new(
|
||||
account_id: @message.account_id,
|
||||
file_type: file_content_type,
|
||||
file: {
|
||||
io: attachment_file,
|
||||
filename: attachment_file.original_filename,
|
||||
content_type: attachment_file.content_type
|
||||
}
|
||||
)
|
||||
end
|
||||
end
|
||||
22
app/services/telegram/send_on_telegram_service.rb
Normal file
22
app/services/telegram/send_on_telegram_service.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
class Telegram::SendOnTelegramService < Base::SendOnChannelService
|
||||
private
|
||||
|
||||
def channel_class
|
||||
Channel::Telegram
|
||||
end
|
||||
|
||||
def perform_reply
|
||||
## send reply to telegram message api
|
||||
# https://core.telegram.org/bots/api#sendmessage
|
||||
message_id = channel.send_message_on_telegram(message.content, conversation[:additional_attributes]['chat_id'])
|
||||
message.update!(source_id: message_id) if message_id.present?
|
||||
end
|
||||
|
||||
def inbox
|
||||
@inbox ||= message.inbox
|
||||
end
|
||||
|
||||
def channel
|
||||
@channel ||= inbox.channel
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user