Getting 400 Error saying “Error While Substituting the templates” while trying to get conversation details from tickets

Here is my requests.json file

{
    "getchat": {
        "schema": {
            "method": "POST",
            "host": "api.openai.com",
            "path": "/v1/chat/completions",
            "headers": {
                "Authorization": "Bearer <my open ai key in plain text>",
                "Content-Type": "application/json"
            }
        }
    },
    "convos": {
        "schema": {
            "method": "GET",
            "host": "karankalsi.freshdesk.com",
            "path": "/api/v2/tickets/<%= context.ticket_id %>/conversations",
            "headers": {
                "Authorization": "Basic <%= encode(my freshdesk key in plain text) %>",
                "Content-Type": "application/json"
            }
        }

    }
}

and here is my app.js file

var client;

init();

async function init() {
  client = await app.initialized();
  client.events.on('app.activated', renderText);
}

function jsonEscape(str)  {
  return str.replace(/\n/g, " ").replace(/\\/g," ")
}

async function renderText() {
  const textElement = document.getElementById('apptext');
  const ticketData = await client.data.get('ticket');
  const {
    ticket: { description_text }
  } = ticketData;

  try {
    const ticketData1 = await client.data.get('ticket');
    const {
      ticket: { id }
    } = ticketData1;
    textElement.innerHTML = id;
    let resp2 = await client.request.invokeTemplate("convos", {
      "context": {
        "ticked_id": id
      }
    });
  
    let convo = " ";
    resp2.forEach(function(json) {
      if(data.incoming == false){
        convo += "Company: " +json.body_text + "\n\n";}
      else{
        convo += "Customer: " +json.body_text + "\n\n";
      }
    });
    let resp = await client.request.invokeTemplate("getchat", {
        "context": {},
        "body": JSON.stringify({
          "model": "gpt-3.5-turbo",
          "messages": [{"role": "system", "content": "You are a helpful assistant."}, 
          {"role": "user", "content": "Provide Summary:\n"+ "Customer: " + description_text + "\n\n"+ convo}
          ]
        })
      });
      var rep_json = JSON.parse(jsonEscape(resp.response));
      var rep2 = rep_json.choices[0].message.content;
      textElement.innerHTML = rep2;

  }
  catch(err){
    textElement.innerHTML = "Error request fail: " + JSON.stringify(err);
  }

}

and the output I get is Status 400 - Error While Substituting the templates

Looks like you have a typo on line 29: “ticked_id” should be “ticket_id” to match what’s in requests.json.

1 Like

Thanks for that , I have fixed it but I am still getting the same error

{
  "status": 400,
  "headers": {},
  "response": "error while substituting templates.",
  "errors": {
    "source": "function(obj) {\nobj || (obj = {});\nvar __t, __p = '';\nwith (obj) {\n__p += '{\"Authorization\":\"Basic ' +\n((__t = ( encode(my freshdesk key) )) == null ? '' : __t) +\n'\",\"Content-Type\":\"application/json\"}';\n\n}\nreturn __p\n}"
  },
  "errorSource": "APP",
  "attempts": 1
}

Hey @Karankalsi,

Quick check on the id value, if that is available and is passed to ticket_id in context. Can you verify that?

If ticket_id is resolved there should be some issue with API substitution. And as part of the best practices for the app, we recommend using Installation Parameters with Request Method.

You can refer to our sample app which does the same - request-method-samples/sample-app at main · freshworks-developers/request-method-samples · GitHub

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.