2021-09-14 10:24:57 +00:00
|
|
|
# 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
|
|
|
|
|
2021-09-09 18:30:52 +00:00
|
|
|
class Telegram::IncomingMessageService
|
|
|
|
include ::FileTypeHelper
|
|
|
|
pattr_initialize [:inbox!, :params!]
|
|
|
|
|
|
|
|
def perform
|
2021-09-14 10:24:57 +00:00
|
|
|
# chatwoot doesn't support group conversations at the moment
|
|
|
|
return unless private_message?
|
|
|
|
|
2021-09-09 18:30:52 +00:00
|
|
|
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
|
|
|
|
|
2021-09-14 10:24:57 +00:00
|
|
|
def private_message?
|
|
|
|
params.dig(:message, :chat, :type) == 'private'
|
|
|
|
end
|
|
|
|
|
2021-09-09 18:30:52 +00:00
|
|
|
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
|
2021-10-13 11:46:14 +00:00
|
|
|
return :image if params[:message][:photo].present? || params.dig(:message, :sticker, :thumb).present?
|
2021-10-12 07:28:30 +00:00
|
|
|
return :audio if params[:message][:voice].present? || params[:message][:audio].present?
|
|
|
|
return :video if params[:message][:video].present?
|
|
|
|
|
|
|
|
file_type(params[:message][:document][:mime_type])
|
2021-09-09 18:30:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def attach_files
|
|
|
|
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,
|
2021-10-12 07:28:30 +00:00
|
|
|
filename: attachment_file,
|
2021-09-09 18:30:52 +00:00
|
|
|
content_type: attachment_file.content_type
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2021-10-12 07:28:30 +00:00
|
|
|
|
|
|
|
def file
|
2021-10-13 11:46:14 +00:00
|
|
|
@file ||= visual_media_params || params[:message][:voice].presence || params[:message][:audio].presence || params[:message][:document].presence
|
|
|
|
end
|
|
|
|
|
|
|
|
def visual_media_params
|
|
|
|
params[:message][:photo].presence&.last || params.dig(:message, :sticker, :thumb).presence || params[:message][:video].presence
|
2021-10-12 07:28:30 +00:00
|
|
|
end
|
2021-09-09 18:30:52 +00:00
|
|
|
end
|