-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add telegram agent #1381
Merged
Merged
add telegram agent #1381
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
require 'httmultiparty' | ||
require 'open-uri' | ||
require 'tempfile' | ||
|
||
module Agents | ||
class TelegramAgent < Agent | ||
cannot_be_scheduled! | ||
cannot_create_events! | ||
no_bulk_receive! | ||
|
||
description <<-MD | ||
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 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 by visiting https://telegram.me/YourHuginnBot | ||
3. Obtain your private `chat_id` from the recently started conversation by visiting https://api.telegram.org/bot<auth_token>/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_ACTIONS = { | ||
text: :sendMessage, | ||
photo: :sendPhoto, | ||
audio: :sendAudio, | ||
document: :sendDocument, | ||
video: :sendVideo | ||
}.freeze | ||
|
||
def telegram_bot_uri(method) | ||
"https://api.telegram.org/bot#{interpolated['auth_token']}/#{method}" | ||
end | ||
|
||
def receive_event(event) | ||
TELEGRAM_ACTIONS.each do |field, method| | ||
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'] | ||
HTTMultiParty.post telegram_bot_uri(method), query: 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)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to ensure that the file gets unlinked afterwards. |
||
file.binmode | ||
file.write open(url).read | ||
file.rewind | ||
file | ||
end | ||
|
||
def unlink_file(file) | ||
file.close | ||
file.unlink | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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([ | ||
{ sendMessage: { text: 'Looks like its going to rain' } }, | ||
{ sendMessage: { 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([{ 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([{ 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([{ 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([{ sendVideo: { 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, that is why I was confused. The description contains the auth_token, but markdown seems to omit it! Thank you for reporting that!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that was it. Thank you for your hard work!