How to make triggers able to get a ticket?

My question is the following, I am using the serverless template in my application and I need to get tickets that are created or updated at freshdesk. It turns out, in the local test environment I was able to take the request that came from freshdesk and verify it, but now that I’m going to the production environment my trigger is not working, he can’t get the request. Do I have to do any specific settings ?

Hi @Lucas_de_Lagos_Pando!

Just a clarification, actually when testing local, you can’t get the ticket created/updated in the production account. To test it local, you need to fire the event using the event simulation screen: http://localhost:10001/web/test

That being said, are your server.js like this:

exports = {
  events: [
    { event: "onTicketCreate", callback: "onTicketCreateCallback" }
  ],
  onTicketCreateCallback: function(payload) {
    console.log("Logging arguments from onTicketCreate event: " + JSON.stringify(payload));
  }
}

Just try to implement it like the example above, install on the account, create a ticket and check the app logs in the account marketplace app logs.
It should print the "Logging arguments from onTicketCreate event: " and the payload.

3 Likes

Hi @samuelpares !

I’m trying to make a post for testing using the website weebhook.site, but when I list a ticket and ask him to make a post on the website quoted, there is simply no request on it … I’ve tried this submission to the webhook.site in many ways. , but I was only successful when I sent a body in json format created by me. How could I get this freshdesk request on this website that I mentioned ?

1 Like

I’m afraid I didn’t understand what you are trying to do.

Are you trying to listen a product event?

Are you trying to listen an external event?

Or are you trying to make an api call sending the ticket as payload?

Hey @Lucas_de_Lagos_Pando

I am not so sure what you are trying to do. If possible can you share with me the code snippet in personal message probably

Hi @samuelpares, @shravan.balasubraman

What I’m trying to do is the following … I’m trying to list a ticket with some specific information after it was created, so I’m using a trigger. After this action I am trying to make a post on the website that I mentioned or webhook.site, but when I try to do that the post on this listed ticket does not work … I confirmed that the trigger is working, because I created a static json body, code below :

onTicketCreateHandler: function (args) {

    const teste ={

      name: "test",

      sobrenome: "test",

      cargo: "não sei"

    }

    const optionsTeste = {

      headers: {

        "Content-Type": "application/json"

      },

      body: JSON.stringify(teste)

    };

    $request.post("https://webhook.site/a0db6782-9c54-4836-b942-9a87976ced71", optionsTeste)
}

and after the ticket was created, the static information that I had created appeared on the webhook.site. My question is: Why can’t I post to the webhook.site with the ticket information I just listed ?

Ok, I understand now.
This should be pretty straight forward.
Please, post here the code that you are using for sending the ticket.

Just remembering, to send the ticket from args, you need to pass in the body like this:
body: JSON.stringify(args.data.ticket)

And you should whitelist the url in manifest.json, example:

  "whitelisted-domains": [
    "https://api.freshchat.com"
  ]

Hi @samuelpares,

The code I am executing is as follows:

onTicketCreateHandler: function (args) {

var apiKey;

    $request.post(`https://backend-homologation.azurewebsites.net/api/test/enconder/${args.iparams.api_key}`, options_api)

      .then(

        function (req_data) {

          apiKey = req_data.response;

          console.log(apiKey)

          if (apiKey !== undefined || apiKey !== null) {

            const optionsVerify = {

              headers: {

                "Authorization": apiKey,

                "Content-Type": "application/json"

              },

            }

            $request.get("https://extcare.freshdesk.com/api/v2/tickets/67", optionsVerify)

              .then(

                function (data) {

                  console.log(data);

                  const options = {

                    body: JSON.stringify(args.data.ticket) 

                  };

                  $request.post("https://webhook.site/a0db6782-9c54-4836-b942-9a87976ced71", options)

                },

                function (error) {

                  console.log(error);

                },

              );

          }

        })

  }

}

Just an explanation in the part of a third api that I am using to get the key of my api and encrypt it, at the beginning of the code I encrypt my api key in base64 and pass it as a parameter in the ticket url being the variable apiKey mine authorization.

Wow man, if the ideia is just to send the ticket just created in the desk account, you don’t need to make all these requests.
You just need to do this:

exports = {
  events: [{ event: "onTicketCreate", callback: "onTicketCreateCallback" }],

  onTicketCreateCallback: function (payload) {
    var options = {
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(payload.data.ticket),
    };
    $request.post("https://webhook.site/a0db6782-9c54-4836-b942-9a87976ced71", options).then(
      function (data) {
        console.log(data);
      },
      function (error) {
        console.error(error);
      }
    );
  },
};

And if you ever need to make a call to a freshdesk endpoint, you can make use of “encode” to encode the api key:

exports = {
  events: [{ event: "onTicketCreate", callback: "onTicketCreateCallback" }],

  onTicketCreateCallback: function (payload) {
    var options = {
      headers = {"Authorization": "Basic <%= encode(iparam.api_key) %>"};
    };
    $request.get("https://subdomain.freshdesk.com/api/v2/tickets" + payload.data.ticket.id, options).then(
      function (data) {
        console.log(data);
      },
      function (error) {
        console.error(error);
      }
    );
  },
};

All of this is explained on the documentation:

2 Likes

Hey @Lucas_de_Lagos_Pando

As part of new security release, you need to whitelist any domains that you are making requests to through $request in server.js

In this case, you manifest.json will look something like this

{
  ...,
  "whitelisted-domains": [
      "https://webhook.site",
      "https://*.freshdesk.com",
      "https://*.azurewebsites.net"
  ]
}

After doing this, please verify if you are able to make API calls

Also, authorization for Freshdesk api should be basic with encoded API key

const optionsVerify = {

              headers: {

                "Authorization": "Basic encode(apiKey)",

                "Content-Type": "application/json"

              },

            }

@samuelpares, @shravan.balasubraman

Thanks for the help, I managed to do what I wanted.

3 Likes