From d45edcde7b4c87df8a3a02679c6af8d33ca7b3b1 Mon Sep 17 00:00:00 2001 From: Alessandro Serpi Date: Fri, 1 Dec 2017 10:44:18 +0100 Subject: [PATCH] Refactor Telegram Agent --- app/models/agents/telegram_agent.rb | 60 ++++++++++++++--------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/app/models/agents/telegram_agent.rb b/app/models/agents/telegram_agent.rb index d170c360..6efb27c5 100644 --- a/app/models/agents/telegram_agent.rb +++ b/app/models/agents/telegram_agent.rb @@ -56,7 +56,7 @@ module Agents form_configurable :parse_mode, type: :array, values: ['', 'html', 'markdown'] def validate_auth_token - HTTMultiParty.post(telegram_bot_uri('getMe'))['ok'] == true + HTTMultiParty.post(telegram_bot_uri('getMe'))['ok'] end def complete_chat_id @@ -94,8 +94,31 @@ module Agents video: :sendVideo }.freeze - def telegram_bot_uri(method) - "https://api.telegram.org/bot#{interpolated['auth_token']}/#{method}" + def configure_params(params) + params[:disable_notification] = interpolated['disable_notification'] if interpolated['disable_notification'].present? + if params.has_key?(:text) + params[:disable_web_page_preview] = interpolated['disable_web_page_preview'] if interpolated['disable_web_page_preview'].present? + params[:parse_mode] = interpolated['parse_mode'] if interpolated['parse_mode'].present? + else + params[:caption] = interpolated['caption'][0..199] if interpolated['caption'].present? + end + + params + end + + def load_field(event, field) + payload = event.payload[field] + return false unless payload.present? + return payload if field == :text + load_file payload + end + + def load_file(url) + file = Tempfile.new [File.basename(url), File.extname(url)] + file.binmode + file.write open(url).read + file.rewind + file end def receive_event(event) @@ -114,24 +137,13 @@ module Agents def send_telegram_message(method, params) params[:chat_id] = interpolated['chat_id'] response = HTTMultiParty.post telegram_bot_uri(method), query: params - if response['ok'] == false + unless response['ok'] error(response) end end - def load_field(event, field) - payload = event.payload[field] - return false unless payload.present? - return payload if field == :text - load_file payload - end - - def load_file(url) - file = Tempfile.new [File.basename(url), File.extname(url)] - file.binmode - file.write open(url).read - file.rewind - file + def telegram_bot_uri(method) + "https://api.telegram.org/bot#{interpolated['auth_token']}/#{method}" end def unlink_file(file) @@ -139,20 +151,6 @@ module Agents file.unlink end - private - - def configure_params(params) - params[:disable_notification] = interpolated['disable_notification'] if interpolated['disable_notification'].present? - if params.has_key?(:text) - params[:disable_web_page_preview] = interpolated['disable_web_page_preview'] if interpolated['disable_web_page_preview'].present? - params[:parse_mode] = interpolated['parse_mode'] if interpolated['parse_mode'].present? - else - params[:caption] = interpolated['caption'][0..199] if interpolated['caption'].present? - end - - params - end - def update_to_complete(update) chat = (update['message'] || update.fetch('channel_post', {})).fetch('chat', {}) {id: chat['id'], text: chat['title'] || "#{chat['first_name']} #{chat['last_name']}"}