feat: Support document file uploads on telegram channel (#9266)

This commit is contained in:
Sojan Jose
2024-05-02 14:05:14 -07:00
committed by GitHub
parent 3488a315d0
commit ac93af6028
4 changed files with 277 additions and 108 deletions

View File

@@ -33,9 +33,9 @@ class Channel::Telegram < ApplicationRecord
end
def send_message_on_telegram(message)
return send_message(message) if message.attachments.empty?
send_attachments(message)
message_id = send_message(message) if message.content.present?
message_id = Telegram::SendAttachmentsService.new(message: message).perform if message.attachments.present?
message_id
end
def get_telegram_profile_image(user_id)
@@ -56,6 +56,23 @@ class Channel::Telegram < ApplicationRecord
"https://api.telegram.org/file/bot#{bot_token}/#{response.parsed_response['result']['file_path']}"
end
def process_error(message, response)
return unless response.parsed_response['ok'] == false
# https://github.com/TelegramBotAPI/errors/tree/master/json
message.external_error = "#{response.parsed_response['error_code']}, #{response.parsed_response['description']}"
message.status = :failed
message.save!
end
def chat_id(message)
message.conversation[:additional_attributes]['chat_id']
end
def reply_to_message_id(message)
message.content_attributes['in_reply_to_external_id']
end
private
def ensure_valid_bot_token
@@ -77,29 +94,12 @@ class Channel::Telegram < ApplicationRecord
errors.add(:bot_token, 'error setting up the webook') unless response.success?
end
def chat_id(message)
message.conversation[:additional_attributes]['chat_id']
end
def reply_to_message_id(message)
message.content_attributes['in_reply_to_external_id']
end
def send_message(message)
response = message_request(chat_id(message), message.content, reply_markup(message), reply_to_message_id(message))
process_error(message, response)
response.parsed_response['result']['message_id'] if response.success?
end
def process_error(message, response)
return unless response.parsed_response['ok'] == false
# https://github.com/TelegramBotAPI/errors/tree/master/json
message.external_error = "#{response.parsed_response['error_code']}, #{response.parsed_response['description']}"
message.status = :failed
message.save!
end
def reply_markup(message)
return unless message.content_type == 'input_select'
@@ -114,41 +114,6 @@ class Channel::Telegram < ApplicationRecord
}.to_json
end
def send_attachments(message)
send_message(message) unless message.content.nil?
telegram_attachments = []
message.attachments.each do |attachment|
telegram_attachment = {}
telegram_attachment[:type] = attachment_type(attachment[:file_type])
telegram_attachment[:media] = attachment.download_url
telegram_attachments << telegram_attachment
end
response = attachments_request(chat_id(message), telegram_attachments, reply_to_message_id(message))
process_error(message, response)
response.parsed_response['result'].first['message_id'] if response.success?
end
def attachment_type(file_type)
file_type_mappings = {
'audio' => 'audio',
'image' => 'photo',
'file' => 'document',
'video' => 'video'
}
file_type_mappings[file_type]
end
def attachments_request(chat_id, attachments, reply_to_message_id)
HTTParty.post("#{telegram_api_url}/sendMediaGroup",
body: {
chat_id: chat_id,
media: attachments.to_json,
reply_to_message_id: reply_to_message_id
})
end
def convert_markdown_to_telegram_html(text)
# ref: https://core.telegram.org/bots/api#html-style