Нет описания http://j1x-huginn.herokuapp.com

telegram_agent.rb 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. require 'httmultiparty'
  2. require 'open-uri'
  3. require 'tempfile'
  4. module Agents
  5. class TelegramAgent < Agent
  6. cannot_be_scheduled!
  7. cannot_create_events!
  8. no_bulk_receive!
  9. description <<-MD
  10. The Telegram Agent receives and collects events and sends them via [Telegram](https://telegram.org/).
  11. 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.
  12. The value of `text` key is sent as a plain text message.
  13. The value of `photo`, `audio`, `document` and `video` keys should be a url whose contents will be sent to you.
  14. **Setup**
  15. 1. Obtain an `auth_token` by [creating a new bot](https://telegram.me/botfather).
  16. 2. Send a private message to your bot by visiting https://telegram.me/YourHuginnBot
  17. 3. Obtain your private `chat_id` from the recently started conversation by visiting https://api.telegram.org/bot`<auth_token>`/getUpdates
  18. MD
  19. def default_options
  20. {
  21. auth_token: 'xxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  22. chat_id: 'xxxxxxxx'
  23. }
  24. end
  25. def validate_options
  26. errors.add(:base, 'auth_token is required') unless options['auth_token'].present?
  27. errors.add(:base, 'chat_id is required') unless options['chat_id'].present?
  28. end
  29. def working?
  30. received_event_without_error? && !recent_error_logs?
  31. end
  32. def receive(incoming_events)
  33. incoming_events.each do |event|
  34. receive_event event
  35. end
  36. end
  37. private
  38. TELEGRAM_ACTIONS = {
  39. text: :sendMessage,
  40. photo: :sendPhoto,
  41. audio: :sendAudio,
  42. document: :sendDocument,
  43. video: :sendVideo
  44. }.freeze
  45. def telegram_bot_uri(method)
  46. "https://api.telegram.org/bot#{interpolated['auth_token']}/#{method}"
  47. end
  48. def receive_event(event)
  49. TELEGRAM_ACTIONS.each do |field, method|
  50. payload = load_field event, field
  51. next unless payload
  52. send_telegram_message method, field => payload
  53. unlink_file payload if payload.is_a? Tempfile
  54. end
  55. end
  56. def send_telegram_message(method, params)
  57. params[:chat_id] = interpolated['chat_id']
  58. HTTMultiParty.post telegram_bot_uri(method), query: params
  59. end
  60. def load_field(event, field)
  61. payload = event.payload[field]
  62. return false unless payload.present?
  63. return payload if field == :text
  64. load_file payload
  65. end
  66. def load_file(url)
  67. file = Tempfile.new [File.basename(url), File.extname(url)]
  68. file.binmode
  69. file.write open(url).read
  70. file.rewind
  71. file
  72. end
  73. def unlink_file(file)
  74. file.close
  75. file.unlink
  76. end
  77. end
  78. end