From adc1dee4fb9660678af73cf6b803308591cdd9d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20K=C3=B6nig?= Date: Sun, 27 Mar 2016 15:56:31 +0200 Subject: [PATCH 1/3] implement basic telegram agent --- Gemfile | 23 ++--- Gemfile.lock | 26 ++++++ app/models/agents/telegram_agent.rb | 92 +++++++++++++++++++ spec/models/agents/telegram_agent_spec.rb | 102 ++++++++++++++++++++++ 4 files changed, 232 insertions(+), 11 deletions(-) create mode 100644 app/models/agents/telegram_agent.rb create mode 100644 spec/models/agents/telegram_agent_spec.rb diff --git a/Gemfile b/Gemfile index 4065a39c..a5e5f01a 100644 --- a/Gemfile +++ b/Gemfile @@ -25,17 +25,18 @@ end # Optional libraries. To conserve RAM, comment out any that you don't need, # then run `bundle` and commit the updated Gemfile and Gemfile.lock. -gem 'twilio-ruby', '~> 3.11.5' # TwilioAgent -gem 'ruby-growl', '~> 4.1.0' # GrowlAgent -gem 'net-ftp-list', '~> 3.2.8' # FtpsiteAgent -gem 'wunderground', '~> 1.2.0' # WeatherAgent -gem 'forecast_io', '~> 2.0.0' # WeatherAgent -gem 'rturk', '~> 2.12.1' # HumanTaskAgent -gem 'hipchat', '~> 1.2.0' # HipchatAgent -gem 'xmpp4r', '~> 0.5.6' # JabberAgent -gem 'mqtt' # MQTTAgent -gem 'slack-notifier', '~> 1.0.0' # SlackAgent -gem 'hypdf', '~> 1.0.7' # PDFInfoAgent +gem 'twilio-ruby', '~> 3.11.5' # TwilioAgent +gem 'ruby-growl', '~> 4.1.0' # GrowlAgent +gem 'net-ftp-list', '~> 3.2.8' # FtpsiteAgent +gem 'wunderground', '~> 1.2.0' # WeatherAgent +gem 'forecast_io', '~> 2.0.0' # WeatherAgent +gem 'rturk', '~> 2.12.1' # HumanTaskAgent +gem 'hipchat', '~> 1.2.0' # HipchatAgent +gem 'xmpp4r', '~> 0.5.6' # JabberAgent +gem 'mqtt' # MQTTAgent +gem 'slack-notifier', '~> 1.0.0' # SlackAgent +gem 'hypdf', '~> 1.0.7' # PDFInfoAgent +gem 'telegram-bot-ruby', '~> 0.4.1' # TelegramAgent # Weibo Agents gem 'weibo_2', github: 'cantino/weibo_2', branch: 'master' diff --git a/Gemfile.lock b/Gemfile.lock index 239fa74c..b2df0366 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -112,6 +112,10 @@ GEM multi_json (>= 1.0.0) aws-sdk-core (2.2.15) jmespath (~> 1.0) + axiom-types (0.1.1) + descendants_tracker (~> 0.0.4) + ice_nine (~> 0.11.0) + thread_safe (~> 0.3, >= 0.3.1) bcrypt (3.1.10) better_errors (1.1.0) coderay (>= 1.0.0) @@ -146,6 +150,8 @@ GEM chronic (0.10.2) cliver (0.3.2) coderay (1.1.0) + coercible (1.0.0) + descendants_tracker (~> 0.0.1) coffee-rails (4.1.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.1.x) @@ -170,6 +176,8 @@ GEM activesupport (>= 3.0, < 5.0) delorean (2.1.0) chronic + descendants_tracker (0.0.4) + thread_safe (~> 0.3, >= 0.3.1) devise (3.5.4) bcrypt (~> 3.0) orm_adapter (~> 0.1) @@ -225,6 +233,8 @@ GEM dotenv (>= 0.7) thor (>= 0.13.6) formatador (0.2.5) + gene_pool (1.4.1) + thread_safe geokit (1.8.5) multi_json (>= 1.3.2) geokit-rails (2.0.1) @@ -281,6 +291,7 @@ GEM hypdf (1.0.7) httmultiparty (= 0.3.10) i18n (0.7.0) + ice_nine (0.11.2) jmespath (1.1.3) jquery-rails (3.1.3) railties (>= 3.0, < 5.0) @@ -371,6 +382,11 @@ GEM multi_json (~> 1.3) omniauth-oauth (~> 1.0) orm_adapter (0.5.0) + persistent_http (1.0.6) + gene_pool (>= 1.3) + persistent_httparty (0.1.2) + httparty (~> 0.9) + persistent_http (< 2) pg (0.18.3) poltergeist (1.8.1) capybara (~> 2.1) @@ -515,6 +531,10 @@ GEM net-ssh (>= 2.8.0) string-scrub (0.0.5) systemu (2.6.4) + telegram-bot-ruby (0.4.1) + httmultiparty + persistent_httparty + virtus term-ansicolor (1.3.0) tins (~> 1.0) therubyracer (0.12.2) @@ -559,6 +579,11 @@ GEM macaddr (~> 1.0) uuidtools (2.1.5) vcr (2.9.2) + virtus (1.0.5) + axiom-types (~> 0.1) + coercible (~> 1.0) + descendants_tracker (~> 0.0, >= 0.0.3) + equalizer (~> 0.0, >= 0.0.9) warden (1.2.4) rack (>= 1.0) webmock (1.17.4) @@ -664,6 +689,7 @@ DEPENDENCIES spring (~> 1.6.3) spring-commands-rspec (~> 1.0.4) string-scrub + telegram-bot-ruby (~> 0.4.1) therubyracer (~> 0.12.2) tumblr_client! twilio-ruby (~> 3.11.5) diff --git a/app/models/agents/telegram_agent.rb b/app/models/agents/telegram_agent.rb new file mode 100644 index 00000000..972a4a9c --- /dev/null +++ b/app/models/agents/telegram_agent.rb @@ -0,0 +1,92 @@ +require 'telegram/bot' +require 'open-uri' +require 'tempfile' + +module Agents + class TelegramAgent < Agent + cannot_be_scheduled! + cannot_create_events! + no_bulk_receive! + + gem_dependency_check { defined?(Telegram) } + + description <<-MD + #{'# Include `telegram-bot-ruby` in your Gemfile to use this Agent!' if dependencies_missing?} + + The Telegram Agent receives and collects events and sends them via [Telegram](https://telegram.org/). + + It is assumed that events have either a `text`, `photo`, `audio`, `document` or `video` key. You can use the EventFormattingAgent if your event does not provide these keys. + + The value of `text` key is sent as a plain text message. + The value of `photo`, `audio`, `document` and `video` keys should be an url which contents are sent to you according to the type. + + **Setup** + + 1. obtain an `auth_token` by [creating a new bot](https://telegram.me/botfather). + 2. [send a private message to your bot](https://telegram.me/YourHuginnBot) + 3. obtain your private `chat_id` [from the recently started conversation](https://api.telegram.org/bot/getUpdates) + MD + + def default_options + { + auth_token: 'xxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', + chat_id: 'xxxxxxxx' + } + end + + def validate_options + errors.add(:base, 'auth_token is required') unless options['auth_token'].present? + errors.add(:base, 'chat_id is required') unless options['chat_id'].present? + end + + def working? + received_event_without_error? && !recent_error_logs? + end + + def receive(incoming_events) + incoming_events.each do |event| + receive_event event + end + end + + private + + TELEGRAM_FIELDS = { + text: :send_message, + photo: :send_photo, + audio: :send_audio, + document: :send_document, + video: :send_video + }.freeze + + def receive_event(event) + TELEGRAM_FIELDS.each do |field, method| + payload = load_field event, field + next unless payload + send_telegram_message method, field => payload + end + end + + def send_telegram_message(method, params) + params[:chat_id] = interpolated['chat_id'] + Telegram::Bot::Client.run interpolated['auth_token'] do |bot| + bot.api.send method, params + 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 + end + end +end diff --git a/spec/models/agents/telegram_agent_spec.rb b/spec/models/agents/telegram_agent_spec.rb new file mode 100644 index 00000000..f8ea4ee6 --- /dev/null +++ b/spec/models/agents/telegram_agent_spec.rb @@ -0,0 +1,102 @@ +require 'rails_helper' + +describe Agents::TelegramAgent do + before do + default_options = { + auth_token: 'xxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', + chat_id: 'xxxxxxxx' + } + @checker = Agents::TelegramAgent.new name: 'Telegram Tester', options: default_options + @checker.user = users(:bob) + @checker.save! + + @sent_messages = [] + stub_methods + end + + def stub_methods + stub.any_instance_of(Agents::TelegramAgent).send_telegram_message do |method, params| + @sent_messages << { method => params } + end + + stub.any_instance_of(Agents::TelegramAgent).load_file do |_url| + :stubbed_file + end + end + + def event_with_payload(payload) + event = Event.new + event.agent = agents(:bob_weather_agent) + event.payload = payload + event.save! + event + end + + describe 'validation' do + before do + expect(@checker).to be_valid + end + + it 'should validate presence of of auth_token' do + @checker.options[:auth_token] = '' + expect(@checker).not_to be_valid + end + + it 'should validate presence of of chat_id' do + @checker.options[:chat_id] = '' + expect(@checker).not_to be_valid + end + end + + describe '#receive' do + it 'processes multiple events properly' do + event_0 = event_with_payload text: 'Looks like its going to rain' + event_1 = event_with_payload text: 'Another text message' + @checker.receive [event_0, event_1] + + expect(@sent_messages).to eq([ + { send_message: { text: 'Looks like its going to rain' } }, + { send_message: { text: 'Another text message' } } + ]) + end + + it 'accepts photo key and uses :send_photo to send the file' do + event = event_with_payload photo: 'https://example.com/image.png' + @checker.receive [event] + + expect(@sent_messages).to eq([{ send_photo: { photo: :stubbed_file } }]) + end + + it 'accepts audio key and uses :send_audio to send the file' do + event = event_with_payload audio: 'https://example.com/sound.mp3' + @checker.receive [event] + + expect(@sent_messages).to eq([{ send_audio: { audio: :stubbed_file } }]) + end + + it 'accepts document key and uses :send_document to send the file' do + event = event_with_payload document: 'https://example.com/document.pdf' + @checker.receive [event] + + expect(@sent_messages).to eq([{ send_document: { document: :stubbed_file } }]) + end + + it 'accepts video key and uses :send_video to send the file' do + event = event_with_payload video: 'https://example.com/video.avi' + @checker.receive [event] + + expect(@sent_messages).to eq([{ send_video: { video: :stubbed_file } }]) + end + end + + describe '#working?' do + it 'is not working without having received an event' do + expect(@checker).not_to be_working + end + + it 'is working after receiving an event without error' do + @checker.last_receive_at = Time.now + expect(@checker).to be_working + end + end +end From c54b68ddff798af9b4fb232d2858bd5a48eed90f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20K=C3=B6nig?= Date: Tue, 29 Mar 2016 08:48:48 +0200 Subject: [PATCH 2/3] remove dependency to telegram client; migrate the according code to httmultiparty --- Gemfile | 24 +++++++------- Gemfile.lock | 40 +++++------------------ app/models/agents/telegram_agent.rb | 31 +++++++++--------- spec/models/agents/telegram_agent_spec.rb | 12 +++---- 4 files changed, 43 insertions(+), 64 deletions(-) diff --git a/Gemfile b/Gemfile index a5e5f01a..edc382d4 100644 --- a/Gemfile +++ b/Gemfile @@ -25,18 +25,17 @@ end # Optional libraries. To conserve RAM, comment out any that you don't need, # then run `bundle` and commit the updated Gemfile and Gemfile.lock. -gem 'twilio-ruby', '~> 3.11.5' # TwilioAgent -gem 'ruby-growl', '~> 4.1.0' # GrowlAgent -gem 'net-ftp-list', '~> 3.2.8' # FtpsiteAgent -gem 'wunderground', '~> 1.2.0' # WeatherAgent -gem 'forecast_io', '~> 2.0.0' # WeatherAgent -gem 'rturk', '~> 2.12.1' # HumanTaskAgent -gem 'hipchat', '~> 1.2.0' # HipchatAgent -gem 'xmpp4r', '~> 0.5.6' # JabberAgent -gem 'mqtt' # MQTTAgent -gem 'slack-notifier', '~> 1.0.0' # SlackAgent -gem 'hypdf', '~> 1.0.7' # PDFInfoAgent -gem 'telegram-bot-ruby', '~> 0.4.1' # TelegramAgent +gem 'twilio-ruby', '~> 3.11.5' # TwilioAgent +gem 'ruby-growl', '~> 4.1.0' # GrowlAgent +gem 'net-ftp-list', '~> 3.2.8' # FtpsiteAgent +gem 'wunderground', '~> 1.2.0' # WeatherAgent +gem 'forecast_io', '~> 2.0.0' # WeatherAgent +gem 'rturk', '~> 2.12.1' # HumanTaskAgent +gem 'hipchat', '~> 1.2.0' # HipchatAgent +gem 'xmpp4r', '~> 0.5.6' # JabberAgent +gem 'mqtt' # MQTTAgent +gem 'slack-notifier', '~> 1.0.0' # SlackAgent +gem 'hypdf', '~> 1.0.10' # PDFInfoAgent # Weibo Agents gem 'weibo_2', github: 'cantino/weibo_2', branch: 'master' @@ -101,6 +100,7 @@ gem 'foreman', '~> 0.63.0' gem 'geokit', '~> 1.8.4' gem 'geokit-rails', '~> 2.0.1' gem 'httparty', '~> 0.13' +gem 'httmultiparty', '~> 0.3.16' gem 'jquery-rails', '~> 3.1.3' gem 'json', '~> 1.8.1' gem 'jsonpath', '~> 0.5.6' diff --git a/Gemfile.lock b/Gemfile.lock index b2df0366..73de03d6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -112,10 +112,6 @@ GEM multi_json (>= 1.0.0) aws-sdk-core (2.2.15) jmespath (~> 1.0) - axiom-types (0.1.1) - descendants_tracker (~> 0.0.4) - ice_nine (~> 0.11.0) - thread_safe (~> 0.3, >= 0.3.1) bcrypt (3.1.10) better_errors (1.1.0) coderay (>= 1.0.0) @@ -150,8 +146,6 @@ GEM chronic (0.10.2) cliver (0.3.2) coderay (1.1.0) - coercible (1.0.0) - descendants_tracker (~> 0.0.1) coffee-rails (4.1.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.1.x) @@ -176,8 +170,6 @@ GEM activesupport (>= 3.0, < 5.0) delorean (2.1.0) chronic - descendants_tracker (0.0.4) - thread_safe (~> 0.3, >= 0.3.1) devise (3.5.4) bcrypt (~> 3.0) orm_adapter (~> 0.1) @@ -233,8 +225,6 @@ GEM dotenv (>= 0.7) thor (>= 0.13.6) formatador (0.2.5) - gene_pool (1.4.1) - thread_safe geokit (1.8.5) multi_json (>= 1.3.2) geokit-rails (2.0.1) @@ -277,21 +267,22 @@ GEM hipchat (1.2.0) httparty hpricot (0.8.6) - httmultiparty (0.3.10) + httmultiparty (0.3.16) httparty (>= 0.7.3) + mimemagic multipart-post http (0.6.4) http_parser.rb (~> 0.6.0) http-cookie (1.0.2) domain_name (~> 0.5) http_parser.rb (0.6.0) - httparty (0.13.1) + httparty (0.13.7) json (~> 1.8) multi_xml (>= 0.5.2) - hypdf (1.0.7) - httmultiparty (= 0.3.10) + hypdf (1.0.10) + httmultiparty (~> 0.3) + httparty (~> 0.13) i18n (0.7.0) - ice_nine (0.11.2) jmespath (1.1.3) jquery-rails (3.1.3) railties (>= 3.0, < 5.0) @@ -329,6 +320,7 @@ GEM thread_safe (~> 0.3, >= 0.3.1) method_source (0.8.2) mime-types (2.99.1) + mimemagic (0.3.1) mini_magick (4.2.3) mini_portile2 (2.0.0) minitest (5.8.4) @@ -382,11 +374,6 @@ GEM multi_json (~> 1.3) omniauth-oauth (~> 1.0) orm_adapter (0.5.0) - persistent_http (1.0.6) - gene_pool (>= 1.3) - persistent_httparty (0.1.2) - httparty (~> 0.9) - persistent_http (< 2) pg (0.18.3) poltergeist (1.8.1) capybara (~> 2.1) @@ -531,10 +518,6 @@ GEM net-ssh (>= 2.8.0) string-scrub (0.0.5) systemu (2.6.4) - telegram-bot-ruby (0.4.1) - httmultiparty - persistent_httparty - virtus term-ansicolor (1.3.0) tins (~> 1.0) therubyracer (0.12.2) @@ -579,11 +562,6 @@ GEM macaddr (~> 1.0) uuidtools (2.1.5) vcr (2.9.2) - virtus (1.0.5) - axiom-types (~> 0.1) - coercible (~> 1.0) - descendants_tracker (~> 0.0, >= 0.0.3) - equalizer (~> 0.0, >= 0.0.9) warden (1.2.4) rack (>= 1.0) webmock (1.17.4) @@ -641,8 +619,9 @@ DEPENDENCIES guard-rspec (~> 4.6.4) haversine hipchat (~> 1.2.0) + httmultiparty (~> 0.3.16) httparty (~> 0.13) - hypdf (~> 1.0.7) + hypdf (~> 1.0.10) jquery-rails (~> 3.1.3) json (~> 1.8.1) jsonpath (~> 0.5.6) @@ -689,7 +668,6 @@ DEPENDENCIES spring (~> 1.6.3) spring-commands-rspec (~> 1.0.4) string-scrub - telegram-bot-ruby (~> 0.4.1) therubyracer (~> 0.12.2) tumblr_client! twilio-ruby (~> 3.11.5) diff --git a/app/models/agents/telegram_agent.rb b/app/models/agents/telegram_agent.rb index 972a4a9c..3446f610 100644 --- a/app/models/agents/telegram_agent.rb +++ b/app/models/agents/telegram_agent.rb @@ -1,18 +1,17 @@ -require 'telegram/bot' +require 'httmultiparty' require 'open-uri' require 'tempfile' module Agents class TelegramAgent < Agent + include HTTMultiParty + base_uri 'https://api.telegram.org/' + cannot_be_scheduled! cannot_create_events! no_bulk_receive! - gem_dependency_check { defined?(Telegram) } - description <<-MD - #{'# Include `telegram-bot-ruby` in your Gemfile to use this Agent!' if dependencies_missing?} - The Telegram Agent receives and collects events and sends them via [Telegram](https://telegram.org/). It is assumed that events have either a `text`, `photo`, `audio`, `document` or `video` key. You can use the EventFormattingAgent if your event does not provide these keys. @@ -51,16 +50,20 @@ module Agents private - TELEGRAM_FIELDS = { - text: :send_message, - photo: :send_photo, - audio: :send_audio, - document: :send_document, - video: :send_video + TELEGRAM_ACTIONS = { + text: :sendMessage, + photo: :sendPhoto, + audio: :sendAudio, + document: :sendDocument, + video: :sendVideo }.freeze + def telegram_bot_uri(method) + "/bot#{interpolated['auth_token']}/#{method}" + end + def receive_event(event) - TELEGRAM_FIELDS.each do |field, method| + TELEGRAM_ACTIONS.each do |field, method| payload = load_field event, field next unless payload send_telegram_message method, field => payload @@ -69,9 +72,7 @@ module Agents def send_telegram_message(method, params) params[:chat_id] = interpolated['chat_id'] - Telegram::Bot::Client.run interpolated['auth_token'] do |bot| - bot.api.send method, params - end + TelegramAgent.post telegram_bot_uri(method), query: params end def load_field(event, field) diff --git a/spec/models/agents/telegram_agent_spec.rb b/spec/models/agents/telegram_agent_spec.rb index f8ea4ee6..f50a318a 100644 --- a/spec/models/agents/telegram_agent_spec.rb +++ b/spec/models/agents/telegram_agent_spec.rb @@ -55,8 +55,8 @@ describe Agents::TelegramAgent do @checker.receive [event_0, event_1] expect(@sent_messages).to eq([ - { send_message: { text: 'Looks like its going to rain' } }, - { send_message: { text: 'Another text message' } } + { sendMessage: { text: 'Looks like its going to rain' } }, + { sendMessage: { text: 'Another text message' } } ]) end @@ -64,28 +64,28 @@ describe Agents::TelegramAgent do event = event_with_payload photo: 'https://example.com/image.png' @checker.receive [event] - expect(@sent_messages).to eq([{ send_photo: { photo: :stubbed_file } }]) + expect(@sent_messages).to eq([{ sendPhoto: { photo: :stubbed_file } }]) end it 'accepts audio key and uses :send_audio to send the file' do event = event_with_payload audio: 'https://example.com/sound.mp3' @checker.receive [event] - expect(@sent_messages).to eq([{ send_audio: { audio: :stubbed_file } }]) + expect(@sent_messages).to eq([{ sendAudio: { audio: :stubbed_file } }]) end it 'accepts document key and uses :send_document to send the file' do event = event_with_payload document: 'https://example.com/document.pdf' @checker.receive [event] - expect(@sent_messages).to eq([{ send_document: { document: :stubbed_file } }]) + expect(@sent_messages).to eq([{ sendDocument: { document: :stubbed_file } }]) end it 'accepts video key and uses :send_video to send the file' do event = event_with_payload video: 'https://example.com/video.avi' @checker.receive [event] - expect(@sent_messages).to eq([{ send_video: { video: :stubbed_file } }]) + expect(@sent_messages).to eq([{ sendVideo: { video: :stubbed_file } }]) end end From 2995029890bcc5cee8e9612e90607dc034ac3d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20K=C3=B6nig?= Date: Wed, 30 Mar 2016 09:28:05 +0200 Subject: [PATCH 3/3] Minor refactoring regarding feedback of @cantino: - minor improvements of the description - use HTTMultiParty directly, instead of including it - ensure that tempfiles are closed and unlinked --- app/models/agents/telegram_agent.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/app/models/agents/telegram_agent.rb b/app/models/agents/telegram_agent.rb index 3446f610..93b8e497 100644 --- a/app/models/agents/telegram_agent.rb +++ b/app/models/agents/telegram_agent.rb @@ -4,9 +4,6 @@ require 'tempfile' module Agents class TelegramAgent < Agent - include HTTMultiParty - base_uri 'https://api.telegram.org/' - cannot_be_scheduled! cannot_create_events! no_bulk_receive! @@ -17,13 +14,13 @@ module Agents It is assumed that events have either a `text`, `photo`, `audio`, `document` or `video` key. You can use the EventFormattingAgent if your event does not provide these keys. The value of `text` key is sent as a plain text message. - The value of `photo`, `audio`, `document` and `video` keys should be an url which contents are sent to you according to the type. + The value of `photo`, `audio`, `document` and `video` keys should be a url whose contents will be sent to you. **Setup** - 1. obtain an `auth_token` by [creating a new bot](https://telegram.me/botfather). - 2. [send a private message to your bot](https://telegram.me/YourHuginnBot) - 3. obtain your private `chat_id` [from the recently started conversation](https://api.telegram.org/bot/getUpdates) + 1. Obtain an `auth_token` by [creating a new bot](https://telegram.me/botfather). + 2. Send a private message to your bot by visiting https://telegram.me/YourHuginnBot + 3. Obtain your private `chat_id` from the recently started conversation by visiting https://api.telegram.org/bot/getUpdates MD def default_options @@ -59,7 +56,7 @@ module Agents }.freeze def telegram_bot_uri(method) - "/bot#{interpolated['auth_token']}/#{method}" + "https://api.telegram.org/bot#{interpolated['auth_token']}/#{method}" end def receive_event(event) @@ -67,12 +64,13 @@ module Agents payload = load_field event, field next unless payload send_telegram_message method, field => payload + unlink_file payload if payload.is_a? Tempfile end end def send_telegram_message(method, params) params[:chat_id] = interpolated['chat_id'] - TelegramAgent.post telegram_bot_uri(method), query: params + HTTMultiParty.post telegram_bot_uri(method), query: params end def load_field(event, field) @@ -89,5 +87,10 @@ module Agents file.rewind file end + + def unlink_file(file) + file.close + file.unlink + end end end