mirror of
https://github.com/Fishwaldo/huginn.git
synced 2025-03-15 19:31:26 +00:00
Added Event Formatting Agent
This commit is contained in:
parent
960876885a
commit
e3aa18b3a0
1 changed files with 61 additions and 0 deletions
61
app/models/agents/event_formatting_agent.rb
Normal file
61
app/models/agents/event_formatting_agent.rb
Normal file
|
@ -0,0 +1,61 @@
|
|||
module Agents
|
||||
class EventFormattingAgent < Agent
|
||||
cannot_be_scheduled!
|
||||
|
||||
description <<-MD
|
||||
Event Formatting Agent allows you to format your events in any way you prefer.
|
||||
For example if you have an event like
|
||||
|
||||
{
|
||||
:content => {
|
||||
:text => "Lorem ipsum dolor sit amet"
|
||||
},
|
||||
:fields => "consectetur adipisicing elit",
|
||||
:data => "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
|
||||
}
|
||||
|
||||
|
||||
And other agents which are receiving this event, say, Twilio Agent expects `message` key , and Digest Email Agent which expects a `subject` key, then you can reformat this event by filling `Options` in the following way.
|
||||
|
||||
message => $.content.text
|
||||
subject => $.fields
|
||||
|
||||
Values must be provided in JSONPath. You can add as many keys as you like.
|
||||
|
||||
Then, event generated by Event Formatting Agent will be like
|
||||
|
||||
{
|
||||
:message => "Lorem ipsum dolor sit amet"
|
||||
:subject => "consectetur adipisicing elit"
|
||||
}
|
||||
|
||||
MD
|
||||
|
||||
event_description <<-MD
|
||||
|
||||
User defined
|
||||
MD
|
||||
|
||||
def default_options
|
||||
{
|
||||
:message => "$.data.fields.content"
|
||||
}
|
||||
end
|
||||
|
||||
def working?
|
||||
true
|
||||
end
|
||||
|
||||
def receive(incoming_events)
|
||||
incoming_events.each do |event|
|
||||
formatted_event = {}
|
||||
options.each_pair do |key,value|
|
||||
formatted_event[key] = Utils.values_at event.payload,value
|
||||
end
|
||||
formatted_event[:agent] = Agent.find(event.agent_id).type.slice! 8..-1
|
||||
formatted_event[:created_at] = event.created_at
|
||||
create_event :payload => formatted_event
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue