router_function_listTemplate.js


const kei = require('../../lib/sendMessage');
const db = require('../../db/db');
const { KeiLog } = require('../../lib/Logger');
const konfig = require('../../config.js');
const axios = require('axios');

/**
 * Module for listing message templates from WhatsApp Business API.
 *@module list_template
 * @param {object} app - The Express application object.
 */
module.exports = async function (app) {
  app.get("/list_template", async (req, res) => {
    try {
      let waba_id = req.query.waba_id;
      if (!waba_id) {
        waba_id = konfig.waba_id;
      }

      console.log("waba_id from query:", req.query.waba_id);
      console.log("waba_id used:", waba_id);
      console.log("konfig.waba_id:", konfig.waba_id);

      const url = `https://graph.facebook.com/v16.0/${waba_id}/message_templates`;
      console.log("URL being used:", url);

      const result = await axios.get(url, {
        headers: {
          "Content-Type": "application/json",
          "Authorization": "Bearer " + konfig.token
        }
      });

    if (req.query.name_template) {
      let data = result.data.data;
      let template = [];
      for (let i = 0; i < data.length; i++) {
        if (data[i].name == req.query.name_template) {
          template.push(data[i]);
        }
      }
      console.log(template)
      res.send(template);
  
    } else {

      res.send(result.data.data);
    }
  } catch (error) {
    console.log(error)
    res.send(error?.response?.data);
  }
  });


};