|  |  | @@ -4,9 +4,6 @@ require 'tempfile' | 
            
            
              | 4 | 4 |   | 
            
            
              | 5 | 5 |  module Agents | 
            
            
              | 6 | 6 |    class TelegramAgent < Agent | 
            
            
              | 7 |  | -    include HTTMultiParty | 
            
            
              | 8 |  | -    base_uri 'https://api.telegram.org/' | 
            
            
              | 9 |  | - | 
            
            
              | 10 | 7 |      cannot_be_scheduled! | 
            
            
              | 11 | 8 |      cannot_create_events! | 
            
            
              | 12 | 9 |      no_bulk_receive! | 
            
            
            
            
              |  |  | @@ -17,13 +14,13 @@ module Agents | 
            
            
              | 17 | 14 |        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. | 
            
            
              | 18 | 15 |   | 
            
            
              | 19 | 16 |        The value of `text` key is sent as a plain text message. | 
            
            
              | 20 |  | -      The value of `photo`, `audio`, `document` and `video` keys should be an url which contents are sent to you according to the type. | 
            
            
              |  | 17 | +      The value of `photo`, `audio`, `document` and `video` keys should be a url whose contents will be sent to you. | 
            
            
              | 21 | 18 |   | 
            
            
              | 22 | 19 |        **Setup** | 
            
            
              | 23 | 20 |   | 
            
            
              | 24 |  | -      1. obtain an `auth_token` by [creating a new bot](https://telegram.me/botfather). | 
            
            
              | 25 |  | -      2. [send a private message to your bot](https://telegram.me/YourHuginnBot) | 
            
            
              | 26 |  | -      3. obtain your private `chat_id` [from the recently started conversation](https://api.telegram.org/bot<auth_token>/getUpdates) | 
            
            
              |  | 21 | +      1. Obtain an `auth_token` by [creating a new bot](https://telegram.me/botfather). | 
            
            
              |  | 22 | +      2. Send a private message to your bot by visiting https://telegram.me/YourHuginnBot | 
            
            
              |  | 23 | +      3. Obtain your private `chat_id` from the recently started conversation by visiting https://api.telegram.org/bot<auth_token>/getUpdates | 
            
            
              | 27 | 24 |      MD | 
            
            
              | 28 | 25 |   | 
            
            
              | 29 | 26 |      def default_options | 
            
            
            
            
              |  |  | @@ -59,7 +56,7 @@ module Agents | 
            
            
              | 59 | 56 |      }.freeze | 
            
            
              | 60 | 57 |   | 
            
            
              | 61 | 58 |      def telegram_bot_uri(method) | 
            
            
              | 62 |  | -      "/bot#{interpolated['auth_token']}/#{method}" | 
            
            
              |  | 59 | +      "https://api.telegram.org/bot#{interpolated['auth_token']}/#{method}" | 
            
            
              | 63 | 60 |      end | 
            
            
              | 64 | 61 |   | 
            
            
              | 65 | 62 |      def receive_event(event) | 
            
            
            
            
              |  |  | @@ -67,12 +64,13 @@ module Agents | 
            
            
              | 67 | 64 |          payload = load_field event, field | 
            
            
              | 68 | 65 |          next unless payload | 
            
            
              | 69 | 66 |          send_telegram_message method, field => payload | 
            
            
              |  | 67 | +        unlink_file payload if payload.is_a? Tempfile | 
            
            
              | 70 | 68 |        end | 
            
            
              | 71 | 69 |      end | 
            
            
              | 72 | 70 |   | 
            
            
              | 73 | 71 |      def send_telegram_message(method, params) | 
            
            
              | 74 | 72 |        params[:chat_id] = interpolated['chat_id'] | 
            
            
              | 75 |  | -      TelegramAgent.post telegram_bot_uri(method), query: params | 
            
            
              |  | 73 | +      HTTMultiParty.post telegram_bot_uri(method), query: params | 
            
            
              | 76 | 74 |      end | 
            
            
              | 77 | 75 |   | 
            
            
              | 78 | 76 |      def load_field(event, field) | 
            
            
            
            
              |  |  | @@ -89,5 +87,10 @@ module Agents | 
            
            
              | 89 | 87 |        file.rewind | 
            
            
              | 90 | 88 |        file | 
            
            
              | 91 | 89 |      end | 
            
            
              |  | 90 | + | 
            
            
              |  | 91 | +    def unlink_file(file) | 
            
            
              |  | 92 | +      file.close | 
            
            
              |  | 93 | +      file.unlink | 
            
            
              |  | 94 | +    end | 
            
            
              | 92 | 95 |    end | 
            
            
              | 93 | 96 |  end |