Extract mailgun JSON webhook

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Extract mailgun JSON webhook

Laurent Gigon
Hi,
I'm trying to get recipient email address of Mailgun webhook. Here is
the format received :

array: ({"signature": {"timestamp": "1541002916", "token":
"9065c09fa611956c348d6f6d55dc76e26dcc4dc76e5b4b70ca", "signature":
"981875bce27c3a05ef2ba581218b4a378b0f263a1a8a13621f3433e11b1fe0a6"},
"event-data": {"tags": ["my_tag_1", "my_tag_2"], "timestamp":
1521233123.501324, "envelope": {"sending-ip": "173.193.210.33"},
"log-level": "warn", "id": "-Agny091SquKnsrW2NEKUA", "campaigns": [],
"user-variables": {"my_var_1": "Mailgun Variable #1", "my-var-2":
"awesome"}, "flags": {"is-test-mode": false}, "message": {"headers":
{"to": "Alice <[hidden email]>", "message-id":
"[hidden email]", "from": "Bob
<[hidden email]>", "subject": "Test complained webhook"},
"attachments": [], "size": 111}, "recipient": "[hidden email]",
"event": "complained"}})

So I use that script (Thanks Steve Piercy btw :) ) but unable to
retreive recipient address :

local('json')=client_params;
     iterate(decode_json(#json), local('i'));

     if(#json -> size == 1);
         #json = #json -> get(1);
         #json = decode_json(#json);
     else;
         // handle error
     /if;

     if(valid_email(#json -> find('recipient')));
             var('email')=#json -> find('recipient');
var('email');

     else;
         // handle error
     /if;

/iterate;


Laurent

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Extract mailgun JSON webhook

Jolle Carlestam-2
local(
        input = `{"signature": {"timestamp": "1541002916", "token":
"9065c09fa611956c348d6f6d55dc76e26dcc4dc76e5b4b70ca", "signature":
"981875bce27c3a05ef2ba581218b4a378b0f263a1a8a13621f3433e11b1fe0a6"},
"event-data": {"tags": ["my_tag_1", "my_tag_2"], "timestamp":
1521233123.501324, "envelope": {"sending-ip": "173.193.210.33"},
"log-level": "warn", "id": "-Agny091SquKnsrW2NEKUA", "campaigns": [],
"user-variables": {"my_var_1": "Mailgun Variable #1", "my-var-2":
"awesome"}, "flags": {"is-test-mode": false}, "message": {"headers":
{"to": "Alice <[hidden email]>", "message-id":
"[hidden email]", "from": "Bob
<[hidden email]>", "subject": "Test complained webhook"},
"attachments": [], "size": 111}, "recipient": "[hidden email]",
"event": "complained"}}`,
        json = json_decode(#input)
)

(#json->find('event-data') or map)->find('recipient’)

This is Lasso 9 code.

HDB
Jolle



> 31 okt. 2018 kl. 17:49 skrev Laurent G <[hidden email]>:
>
> Hi,
> I'm trying to get recipient email address of Mailgun webhook. Here is
> the format received :
>
> array: ({"signature": {"timestamp": "1541002916", "token":
> "9065c09fa611956c348d6f6d55dc76e26dcc4dc76e5b4b70ca", "signature":
> "981875bce27c3a05ef2ba581218b4a378b0f263a1a8a13621f3433e11b1fe0a6"},
> "event-data": {"tags": ["my_tag_1", "my_tag_2"], "timestamp":
> 1521233123.501324, "envelope": {"sending-ip": "173.193.210.33"},
> "log-level": "warn", "id": "-Agny091SquKnsrW2NEKUA", "campaigns": [],
> "user-variables": {"my_var_1": "Mailgun Variable #1", "my-var-2":
> "awesome"}, "flags": {"is-test-mode": false}, "message": {"headers":
> {"to": "Alice <[hidden email]>", "message-id":
> "[hidden email]", "from": "Bob
> <[hidden email]>", "subject": "Test complained webhook"},
> "attachments": [], "size": 111}, "recipient": "[hidden email]",
> "event": "complained"}})
>
> So I use that script (Thanks Steve Piercy btw :) ) but unable to
> retreive recipient address :
>
> local('json')=client_params;
>     iterate(decode_json(#json), local('i'));
>
>     if(#json -> size == 1);
>         #json = #json -> get(1);
>         #json = decode_json(#json);
>     else;
>         // handle error
>     /if;
>
>     if(valid_email(#json -> find('recipient')));
>             var('email')=#json -> find('recipient');
> var('email');
>
>     else;
>         // handle error
>     /if;
>
> /iterate;
>
>
> Laurent
>
> #############################################################
>
> This message is sent to you because you are subscribed to
>  the mailing list Lasso [hidden email]
> Official list archives available at http://www.lassotalk.com
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>


#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Extract mailgun JSON webhook

Jolle Carlestam-2
Let me add a more verbose explanation. The input your’e dealing with consists mostly of what Lasso refers to as maps.
Example:
{"mykey1": "My Value 1", "mykey2": "My Value 2”}
In Javascript/JSON lingo this is an object. The Lasso equivalent is called map. Writing the exact same thing in Lasso would look like this:
map("mykey1" = "My Value 1", ”mykey2” = "My Value 2”)
An object/map needs at least one key pointing to a value. Well, it doesn’t NEED it, an object/map can be empty. But it can’t consist of either only a key, or only a value.

Something that looks like this:
["value 1", "Value 2”]
Is what in Javascript/JSON is called an array. It so happens that it’s called an array in Lasso as well. This is how it’s done in Lasso:
array("value 1", "Value 2”)

Now, objects/maps are not positional. That means you can’t do:
map("mykey1" = "My Value 1", ”mykey2” = "My Value 2”)->get(1)
Because there’s no guarantee that what you’re looking for is placed in the first position.
to get a value in a map you have to find it:
map("mykey1" = "My Value 1", ”mykey2” = "My Value 2”)->get(’mykey1’)
-> My Value 1
Due to the non positional nature of a map it is also not a good idea to iterate it to get something based on the iteration count.

An array on the other hand is maintaining the position the items were placed in it. Thus:
array("value 1", "Value 2”)->get(1)
is perfectly legal. Provided there actually is something in the first position. If not you’ll get an error.

The input you are dealing with consisted of a map with maps. Those maps in turn contained other maps, or values. And in some cases arrays, both empty and with content. Track the { (Here starts an object) and [ (here starts an array).

To find out what to look for I first did a simple
#json->keys
-> signature, event-data
I could then followup that with a
#json->find('event-data’)->keys
-> campaigns, envelope, event, flags, id, log-level, message, recipient, tags, timestamp, user-variables

Thus the construct
(#json->find('event-data') or map)->find('recipient’)
results in
-> [hidden email]

The 'or map' part is to make sure that the second part won’t trigger an error should there be no event-data to look for.
If it’s not there #json->find('event-data’) will return void. And void->find('recipient’) doesn’t work.


Have fun!

Jolle

> 31 okt. 2018 kl. 18:03 skrev Jolle Carlestam <[hidden email]>:
>
> local(
> input = `{"signature": {"timestamp": "1541002916", "token":
> "9065c09fa611956c348d6f6d55dc76e26dcc4dc76e5b4b70ca", "signature":
> "981875bce27c3a05ef2ba581218b4a378b0f263a1a8a13621f3433e11b1fe0a6"},
> "event-data": {"tags": ["my_tag_1", "my_tag_2"], "timestamp":
> 1521233123.501324, "envelope": {"sending-ip": "173.193.210.33"},
> "log-level": "warn", "id": "-Agny091SquKnsrW2NEKUA", "campaigns": [],
> "user-variables": {"my_var_1": "Mailgun Variable #1", "my-var-2":
> "awesome"}, "flags": {"is-test-mode": false}, "message": {"headers":
> {"to": "Alice <[hidden email]>", "message-id":
> "[hidden email]", "from": "Bob
> <[hidden email]>", "subject": "Test complained webhook"},
> "attachments": [], "size": 111}, "recipient": "[hidden email]",
> "event": "complained"}}`,
> json = json_decode(#input)
> )
>
> (#json->find('event-data') or map)->find('recipient’)
>
> This is Lasso 9 code.
>
> HDB
> Jolle
>
>
>
>> 31 okt. 2018 kl. 17:49 skrev Laurent G <[hidden email]>:
>>
>> Hi,
>> I'm trying to get recipient email address of Mailgun webhook. Here is
>> the format received :
>>
>> array: ({"signature": {"timestamp": "1541002916", "token":
>> "9065c09fa611956c348d6f6d55dc76e26dcc4dc76e5b4b70ca", "signature":
>> "981875bce27c3a05ef2ba581218b4a378b0f263a1a8a13621f3433e11b1fe0a6"},
>> "event-data": {"tags": ["my_tag_1", "my_tag_2"], "timestamp":
>> 1521233123.501324, "envelope": {"sending-ip": "173.193.210.33"},
>> "log-level": "warn", "id": "-Agny091SquKnsrW2NEKUA", "campaigns": [],
>> "user-variables": {"my_var_1": "Mailgun Variable #1", "my-var-2":
>> "awesome"}, "flags": {"is-test-mode": false}, "message": {"headers":
>> {"to": "Alice <[hidden email]>", "message-id":
>> "[hidden email]", "from": "Bob
>> <[hidden email]>", "subject": "Test complained webhook"},
>> "attachments": [], "size": 111}, "recipient": "[hidden email]",
>> "event": "complained"}})
>>
>> So I use that script (Thanks Steve Piercy btw :) ) but unable to
>> retreive recipient address :
>>
>> local('json')=client_params;
>>    iterate(decode_json(#json), local('i'));
>>
>>    if(#json -> size == 1);
>>        #json = #json -> get(1);
>>        #json = decode_json(#json);
>>    else;
>>        // handle error
>>    /if;
>>
>>    if(valid_email(#json -> find('recipient')));
>>            var('email')=#json -> find('recipient');
>> var('email');
>>
>>    else;
>>        // handle error
>>    /if;
>>
>> /iterate;
>>
>>
>> Laurent
>>
>> #############################################################
>>
>> This message is sent to you because you are subscribed to
>> the mailing list Lasso [hidden email]
>> Official list archives available at http://www.lassotalk.com
>> To unsubscribe, E-mail to: <[hidden email]>
>> Send administrative queries to  <[hidden email]>
>
>
> #############################################################
>
> This message is sent to you because you are subscribed to
>  the mailing list Lasso [hidden email]
> Official list archives available at http://www.lassotalk.com
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>


#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Extract mailgun JSON webhook

Jolle Carlestam-2
Sorry, copy/paste typo. Should be
map("mykey1" = "My Value 1", ”mykey2” = "My Value 2”)->find(’mykey1’)

> 31 okt. 2018 kl. 18:33 skrev Jolle Carlestam <[hidden email]>:
>
> map("mykey1" = "My Value 1", ”mykey2” = "My Value 2”)->get(’mykey1’)


#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Extract mailgun JSON webhook

Carl Ketterling
In reply to this post by Jolle Carlestam-2
Thank you for this lesson.  This is one topic I’ve had a tough time understanding, and your explanation helps tremendously!

Carl

> On Oct 31, 2018, at 12:33 PM, Jolle Carlestam <[hidden email]> wrote:
>
> Let me add a more verbose explanation. The input your’e dealing with consists mostly of what Lasso refers to as maps.
> Example:
> {"mykey1": "My Value 1", "mykey2": "My Value 2”}
> In Javascript/JSON lingo this is an object. The Lasso equivalent is called map. Writing the exact same thing in Lasso would look like this:
> map("mykey1" = "My Value 1", ”mykey2” = "My Value 2”)
> An object/map needs at least one key pointing to a value. Well, it doesn’t NEED it, an object/map can be empty. But it can’t consist of either only a key, or only a value.
>
> Something that looks like this:
> ["value 1", "Value 2”]
> Is what in Javascript/JSON is called an array. It so happens that it’s called an array in Lasso as well. This is how it’s done in Lasso:
> array("value 1", "Value 2”)
>
> Now, objects/maps are not positional. That means you can’t do:
> map("mykey1" = "My Value 1", ”mykey2” = "My Value 2”)->get(1)
> Because there’s no guarantee that what you’re looking for is placed in the first position.
> to get a value in a map you have to find it:
> map("mykey1" = "My Value 1", ”mykey2” = "My Value 2”)->get(’mykey1’)
> -> My Value 1
> Due to the non positional nature of a map it is also not a good idea to iterate it to get something based on the iteration count.
>
> An array on the other hand is maintaining the position the items were placed in it. Thus:
> array("value 1", "Value 2”)->get(1)
> is perfectly legal. Provided there actually is something in the first position. If not you’ll get an error.
>
> The input you are dealing with consisted of a map with maps. Those maps in turn contained other maps, or values. And in some cases arrays, both empty and with content. Track the { (Here starts an object) and [ (here starts an array).
>
> To find out what to look for I first did a simple
> #json->keys
> -> signature, event-data
> I could then followup that with a
> #json->find('event-data’)->keys
> -> campaigns, envelope, event, flags, id, log-level, message, recipient, tags, timestamp, user-variables
>
> Thus the construct
> (#json->find('event-data') or map)->find('recipient’)
> results in
> -> [hidden email]
>
> The 'or map' part is to make sure that the second part won’t trigger an error should there be no event-data to look for.
> If it’s not there #json->find('event-data’) will return void. And void->find('recipient’) doesn’t work.
>
>
> Have fun!
>
> Jolle
>
>> 31 okt. 2018 kl. 18:03 skrev Jolle Carlestam <[hidden email]>:
>>
>> local(
>> input = `{"signature": {"timestamp": "1541002916", "token":
>> "9065c09fa611956c348d6f6d55dc76e26dcc4dc76e5b4b70ca", "signature":
>> "981875bce27c3a05ef2ba581218b4a378b0f263a1a8a13621f3433e11b1fe0a6"},
>> "event-data": {"tags": ["my_tag_1", "my_tag_2"], "timestamp":
>> 1521233123.501324, "envelope": {"sending-ip": "173.193.210.33"},
>> "log-level": "warn", "id": "-Agny091SquKnsrW2NEKUA", "campaigns": [],
>> "user-variables": {"my_var_1": "Mailgun Variable #1", "my-var-2":
>> "awesome"}, "flags": {"is-test-mode": false}, "message": {"headers":
>> {"to": "Alice <[hidden email]>", "message-id":
>> "[hidden email]", "from": "Bob
>> <[hidden email]>", "subject": "Test complained webhook"},
>> "attachments": [], "size": 111}, "recipient": "[hidden email]",
>> "event": "complained"}}`,
>> json = json_decode(#input)
>> )
>>
>> (#json->find('event-data') or map)->find('recipient’)
>>
>> This is Lasso 9 code.
>>
>> HDB
>> Jolle
>>
>>
>>
>>> 31 okt. 2018 kl. 17:49 skrev Laurent G <[hidden email]>:
>>>
>>> Hi,
>>> I'm trying to get recipient email address of Mailgun webhook. Here is
>>> the format received :
>>>
>>> array: ({"signature": {"timestamp": "1541002916", "token":
>>> "9065c09fa611956c348d6f6d55dc76e26dcc4dc76e5b4b70ca", "signature":
>>> "981875bce27c3a05ef2ba581218b4a378b0f263a1a8a13621f3433e11b1fe0a6"},
>>> "event-data": {"tags": ["my_tag_1", "my_tag_2"], "timestamp":
>>> 1521233123.501324, "envelope": {"sending-ip": "173.193.210.33"},
>>> "log-level": "warn", "id": "-Agny091SquKnsrW2NEKUA", "campaigns": [],
>>> "user-variables": {"my_var_1": "Mailgun Variable #1", "my-var-2":
>>> "awesome"}, "flags": {"is-test-mode": false}, "message": {"headers":
>>> {"to": "Alice <[hidden email]>", "message-id":
>>> "[hidden email]", "from": "Bob
>>> <[hidden email]>", "subject": "Test complained webhook"},
>>> "attachments": [], "size": 111}, "recipient": "[hidden email]",
>>> "event": "complained"}})
>>>
>>> So I use that script (Thanks Steve Piercy btw :) ) but unable to
>>> retreive recipient address :
>>>
>>> local('json')=client_params;
>>>   iterate(decode_json(#json), local('i'));
>>>
>>>   if(#json -> size == 1);
>>>       #json = #json -> get(1);
>>>       #json = decode_json(#json);
>>>   else;
>>>       // handle error
>>>   /if;
>>>
>>>   if(valid_email(#json -> find('recipient')));
>>>           var('email')=#json -> find('recipient');
>>> var('email');
>>>
>>>   else;
>>>       // handle error
>>>   /if;
>>>
>>> /iterate;
>>>
>>>
>>> Laurent
>>>
>>> #############################################################
>>>
>>> This message is sent to you because you are subscribed to
>>> the mailing list Lasso [hidden email]
>>> Official list archives available at http://www.lassotalk.com
>>> To unsubscribe, E-mail to: <[hidden email]>
>>> Send administrative queries to  <[hidden email]>
>>
>>
>> #############################################################
>>
>> This message is sent to you because you are subscribed to
>> the mailing list Lasso [hidden email]
>> Official list archives available at http://www.lassotalk.com
>> To unsubscribe, E-mail to: <[hidden email]>
>> Send administrative queries to  <[hidden email]>
>
>
> #############################################################
>
> This message is sent to you because you are subscribed to
>  the mailing list Lasso [hidden email]
> Official list archives available at http://www.lassotalk.com
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>


#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Extract mailgun JSON webhook

Laurent Gigon
In reply to this post by Jolle Carlestam-2
Thank you Jolle for those explanations, I'm trying to figure out on
Lasso 8.6, not sure it work.
Le mer. 31 oct. 2018 à 13:03, Jolle Carlestam <[hidden email]> a écrit :

>
> local(
>         input = `{"signature": {"timestamp": "1541002916", "token":
> "9065c09fa611956c348d6f6d55dc76e26dcc4dc76e5b4b70ca", "signature":
> "981875bce27c3a05ef2ba581218b4a378b0f263a1a8a13621f3433e11b1fe0a6"},
> "event-data": {"tags": ["my_tag_1", "my_tag_2"], "timestamp":
> 1521233123.501324, "envelope": {"sending-ip": "173.193.210.33"},
> "log-level": "warn", "id": "-Agny091SquKnsrW2NEKUA", "campaigns": [],
> "user-variables": {"my_var_1": "Mailgun Variable #1", "my-var-2":
> "awesome"}, "flags": {"is-test-mode": false}, "message": {"headers":
> {"to": "Alice <[hidden email]>", "message-id":
> "[hidden email]", "from": "Bob
> <[hidden email]>", "subject": "Test complained webhook"},
> "attachments": [], "size": 111}, "recipient": "[hidden email]",
> "event": "complained"}}`,
>         json = json_decode(#input)
> )
>
> (#json->find('event-data') or map)->find('recipient’)
>
> This is Lasso 9 code.
>
> HDB
> Jolle
>
>
>
> > 31 okt. 2018 kl. 17:49 skrev Laurent G <[hidden email]>:
> >
> > Hi,
> > I'm trying to get recipient email address of Mailgun webhook. Here is
> > the format received :
> >
> > array: ({"signature": {"timestamp": "1541002916", "token":
> > "9065c09fa611956c348d6f6d55dc76e26dcc4dc76e5b4b70ca", "signature":
> > "981875bce27c3a05ef2ba581218b4a378b0f263a1a8a13621f3433e11b1fe0a6"},
> > "event-data": {"tags": ["my_tag_1", "my_tag_2"], "timestamp":
> > 1521233123.501324, "envelope": {"sending-ip": "173.193.210.33"},
> > "log-level": "warn", "id": "-Agny091SquKnsrW2NEKUA", "campaigns": [],
> > "user-variables": {"my_var_1": "Mailgun Variable #1", "my-var-2":
> > "awesome"}, "flags": {"is-test-mode": false}, "message": {"headers":
> > {"to": "Alice <[hidden email]>", "message-id":
> > "[hidden email]", "from": "Bob
> > <[hidden email]>", "subject": "Test complained webhook"},
> > "attachments": [], "size": 111}, "recipient": "[hidden email]",
> > "event": "complained"}})
> >
> > So I use that script (Thanks Steve Piercy btw :) ) but unable to
> > retreive recipient address :
> >
> > local('json')=client_params;
> >     iterate(decode_json(#json), local('i'));
> >
> >     if(#json -> size == 1);
> >         #json = #json -> get(1);
> >         #json = decode_json(#json);
> >     else;
> >         // handle error
> >     /if;
> >
> >     if(valid_email(#json -> find('recipient')));
> >             var('email')=#json -> find('recipient');
> > var('email');
> >
> >     else;
> >         // handle error
> >     /if;
> >
> > /iterate;
> >
> >
> > Laurent
> >
> > #############################################################
> >
> > This message is sent to you because you are subscribed to
> >  the mailing list Lasso [hidden email]
> > Official list archives available at http://www.lassotalk.com
> > To unsubscribe, E-mail to: <[hidden email]>
> > Send administrative queries to  <[hidden email]>
>
>
> #############################################################
>
> This message is sent to you because you are subscribed to
>   the mailing list Lasso [hidden email]
> Official list archives available at http://www.lassotalk.com
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Extract mailgun JSON webhook

stevepiercy
In reply to this post by Laurent Gigon
You have a Lasso array (#myarray below) that contains one
element, a JSON object.  That JSON object is not a string, but
should by a bytes type.  Formatted by PyCharm, it looks like this:

{
   "signature": {
     "timestamp": "1541002916",
     "token": "9065c09fa611956c348d6f6d55dc76e26dcc4dc76e5b4b70ca",
     "signature": "981875bce27c3a05ef2ba581218b4a378b0f263a1a8a13621f3433e11b1fe0a6"
   },
   "event-data": {
     "tags": [
       "my_tag_1",
       "my_tag_2"
     ],
     "timestamp": 1521233123.501324,
     "envelope": {
       "sending-ip": "173.193.210.33"
     },
     "log-level": "warn",
     "id": "-Agny091SquKnsrW2NEKUA",
     "campaigns": [],
     "user-variables": {
       "my_var_1": "Mailgun Variable #1",
       "my-var-2": "awesome"
     },
     "flags": {
       "is-test-mode": false
     },
     "message": {
       "headers": {
         "to": "Alice <[hidden email]>",
         "message-id": "[hidden email]",
         "from": "Bob <[hidden email]>",
         "subject": "Test complained webhook"
       },
       "attachments": [],
       "size": 111
     },
     "recipient": "[hidden email]",
     "event": "complained"
   }
}

JSON {} denotes a map, whereas [] denotes an array, in Lasso terminology.

Assuming the original array is always one element in length:

local('json') = #myarray -> get(1);
// convert the json object to a complex nested Lasso type
#json = decode_json(#json);
local('to') = #json -> find('event-data')
                     -> find('message')
                     -> find('headers')
                     -> find('to')

Should return:

Alice <[hidden email]>

 From there you need to use a regex to extract the content of
the <>.

local('email') = string_replaceregexp(#to, -find='^.*<(.*)>$', -replace='\1');

--steve


On 10/31/18 at 12:49 PM, [hidden email] (Laurent G) pronounced:

>Hi,
>I'm trying to get recipient email address of Mailgun webhook. Here is
>the format received :
>
>array: ({"signature": {"timestamp": "1541002916", "token":
>"9065c09fa611956c348d6f6d55dc76e26dcc4dc76e5b4b70ca", "signature":
>"981875bce27c3a05ef2ba581218b4a378b0f263a1a8a13621f3433e11b1fe0a6"},
>"event-data": {"tags": ["my_tag_1", "my_tag_2"], "timestamp":
>1521233123.501324, "envelope": {"sending-ip": "173.193.210.33"},
>"log-level": "warn", "id": "-Agny091SquKnsrW2NEKUA", "campaigns": [],
>"user-variables": {"my_var_1": "Mailgun Variable #1", "my-var-2":
>"awesome"}, "flags": {"is-test-mode": false}, "message": {"headers":
>{"to": "Alice <[hidden email]>", "message-id":
>"[hidden email]", "from": "Bob
><[hidden email]>", "subject": "Test complained webhook"},
>"attachments": [], "size": 111}, "recipient": "[hidden email]",
>"event": "complained"}})
>
>So I use that script (Thanks Steve Piercy btw :) ) but unable to
>retreive recipient address :
>
>local('json')=client_params;
>iterate(decode_json(#json), local('i'));
>
>if(#json -> size == 1);
>#json = #json -> get(1);
>#json = decode_json(#json);
>else;
>// handle error
>/if;
>
>if(valid_email(#json -> find('recipient')));
>var('email')=#json -> find('recipient');
>var('email');
>
>else;
>// handle error
>/if;
>
>/iterate;
>
>
>Laurent
>
>#############################################################
>
>This message is sent to you because you are subscribed to
>the mailing list Lasso [hidden email]
>Official list archives available at http://www.lassotalk.com
>To unsubscribe, E-mail to: <[hidden email]>
>Send administrative queries to  <[hidden email]>

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Steve Piercy              Website Builder              Eugene, OR
<[hidden email]>               <http://www.stevepiercy.com/>


#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Extract mailgun JSON webhook

Jolle Carlestam-2
Why go the more complex route of digging down to the ’to' value and then having to do a regex on the content? It looks way more error prone than going for the ’recipient’ value. I’m not familiar with the setup this data originates from, but what if there’re several recipients? Your regex would fail then. What if the email was sent to cc or bcc instead? That would leave the to value empty but would probably still populate the recipient value.

HDB
Jolle

> 1 nov. 2018 kl. 00:43 skrev Steve Piercy - Website Builder <[hidden email]>:
>
> local('to') = #json -> find('event-data')
>                    -> find('message')
>                    -> find('headers')
>                    -> find('to')
>
> Should return:
>
> Alice <[hidden email]>
>
> From there you need to use a regex to extract the content of the <>.
>
> local('email') = string_replaceregexp(#to, -find='^.*<(.*)>$', -replace='\1');
>


#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Extract mailgun JSON webhook

stevepiercy
Meh, I didn't go to the end and see recipient in the JSON.  So
adjust accordingly.

--steve


On 11/1/18 at 4:18 AM, [hidden email] (Jolle Carlestam) pronounced:

>Why go the more complex route of digging down to the ’to'
>value and then having to do a regex on the content? It looks
>way more error prone than going for the ’recipient’ value.
>I’m not familiar with the setup this data originates from,
>but what if there’re several recipients? Your regex would
>fail then. What if the email was sent to cc or bcc instead?
>That would leave the to value empty but would probably still
>populate the recipient value.
>
>HDB
>Jolle
>
>>1 nov. 2018 kl. 00:43 skrev Steve Piercy - Website Builder <[hidden email]>:
>>
>>local('to') = #json -> find('event-data')
>>-> find('message')
>>-> find('headers')
>>-> find('to')
>>
>>Should return:
>>
>>Alice <[hidden email]>
>>
>>From there you need to use a regex to extract the content of the <>.
>>
>>local('email') = string_replaceregexp(#to, -find='^.*<(.*)>$', -replace='\1');
>>
>
>
>#############################################################
>
>This message is sent to you because you are subscribed to
>the mailing list Lasso [hidden email]
>Official list archives available at http://www.lassotalk.com
>To unsubscribe, E-mail to: <[hidden email]>
>Send administrative queries to  <[hidden email]>

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Steve Piercy              Website Builder              Eugene, OR
<[hidden email]>               <http://www.stevepiercy.com/>


#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Extract mailgun JSON webhook

Laurent Gigon
Thank you Steve and Jolle, it works. Now I'm trying to use iterate
because I'm pretty sure the Mailgun webhook will return multiple
lines, how can I handle that?
Le jeu. 1 nov. 2018 à 02:41, Steve Piercy - Website Builder
<[hidden email]> a écrit :

>
> Meh, I didn't go to the end and see recipient in the JSON.  So
> adjust accordingly.
>
> --steve
>
>
> On 11/1/18 at 4:18 AM, [hidden email] (Jolle Carlestam) pronounced:
>
> >Why go the more complex route of digging down to the ’to'
> >value and then having to do a regex on the content? It looks
> >way more error prone than going for the ’recipient’ value.
> >I’m not familiar with the setup this data originates from,
> >but what if there’re several recipients? Your regex would
> >fail then. What if the email was sent to cc or bcc instead?
> >That would leave the to value empty but would probably still
> >populate the recipient value.
> >
> >HDB
> >Jolle
> >
> >>1 nov. 2018 kl. 00:43 skrev Steve Piercy - Website Builder <[hidden email]>:
> >>
> >>local('to') = #json -> find('event-data')
> >>-> find('message')
> >>-> find('headers')
> >>-> find('to')
> >>
> >>Should return:
> >>
> >>Alice <[hidden email]>
> >>
> >>From there you need to use a regex to extract the content of the <>.
> >>
> >>local('email') = string_replaceregexp(#to, -find='^.*<(.*)>$', -replace='\1');
> >>
> >
> >
> >#############################################################
> >
> >This message is sent to you because you are subscribed to
> >the mailing list Lasso [hidden email]
> >Official list archives available at http://www.lassotalk.com
> >To unsubscribe, E-mail to: <[hidden email]>
> >Send administrative queries to  <[hidden email]>
>
> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> Steve Piercy              Website Builder              Eugene, OR
> <[hidden email]>               <http://www.stevepiercy.com/>
>
>
> #############################################################
>
> This message is sent to you because you are subscribed to
>   the mailing list Lasso [hidden email]
> Official list archives available at http://www.lassotalk.com
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Extract mailgun JSON webhook

Jolle Carlestam-2
1 nov. 2018 kl. 15:57 skrev Laurent G <[hidden email]>:
>
> Thank you Steve and Jolle, it works. Now I'm trying to use iterate
> because I'm pretty sure the Mailgun webhook will return multiple
> lines, how can I handle that?

That entirely depends on how the JSON looks. If it comes with an array wrapper then you iterate on that level, as in basically the first thing you do. If the array of emails is inside a map/object then you first retrieve that array using map->find(’the_array_key’).

Grab the response as raw as you can get it and examine it. Does it start with a [ ? Then it’s an array. Does it start with a { ? It’s a map.

There’s also probably some place where you can read up on the mailgun API. Public APIs tend to come with some kind of documentation.

HDB
Jolle

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Extract mailgun JSON webhook

Laurent Gigon
It seems to be only one line for each webhook so far. I keep you posted.

Here is my code below , Lasso 8.6, thank you again guys for your precious help!

local('json')=client_params;

var('myFile_log') = '/logs/mailgun_log_'+ date_format(server_date ' '
server_time,-format='%q') +'.txt';
Var('File_Contents_Raw') = client_params;


    if(#json -> size == 1);
        #json = #json -> get(1);
        #json = decode_json(#json);


var('email') =  #json -> find('event-data') ->find('recipient');
var('subject') =  #json -> find('event-data') -> find('message')  ->
find('headers') ->find('subject');
var('type') =  #json -> find('event-data') ->find('event');

var('email');

if( Valid_Email(var('email')) == True );

Inline( -Database='mydb',
-Table='mytable',
'email'=var('email'),
'subject'=var('subject'),
'type'=var('type'),
'date'=date_format(Server_Date, -format='%Q'),
-Add);

/Inline;

/if;

    else;

        'Error!';
   /if;



If(File_Exists($myFile_log) == False);
File_Create($myFile_log);
If(File_CurrentError == Error_NoError);
File_Write($myFile_log, $File_Contents_Raw);
Else;
/If;
Else;
File_Chmod($myFile_log, -u='rwx', -g='rwx', -o='rx');
File_Write($myFile_log, $File_Contents_Raw);
/If;


Le jeu. 1 nov. 2018 à 11:03, Jolle Carlestam <[hidden email]> a écrit :

>
> 1 nov. 2018 kl. 15:57 skrev Laurent G <[hidden email]>:
> >
> > Thank you Steve and Jolle, it works. Now I'm trying to use iterate
> > because I'm pretty sure the Mailgun webhook will return multiple
> > lines, how can I handle that?
>
> That entirely depends on how the JSON looks. If it comes with an array wrapper then you iterate on that level, as in basically the first thing you do. If the array of emails is inside a map/object then you first retrieve that array using map->find(’the_array_key’).
>
> Grab the response as raw as you can get it and examine it. Does it start with a [ ? Then it’s an array. Does it start with a { ? It’s a map.
>
> There’s also probably some place where you can read up on the mailgun API. Public APIs tend to come with some kind of documentation.
>
> HDB
> Jolle
>
> #############################################################
>
> This message is sent to you because you are subscribed to
>   the mailing list Lasso [hidden email]
> Official list archives available at http://www.lassotalk.com
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>