static callUserProfileAPI()

in loginconnect_directintegrator/services/graph-api.js [167:201]


  static callUserProfileAPI(senderPsid) {
    return new Promise(function(resolve, reject) {
      let body = [];

      // Send the HTTP request to the Graph API
      request({
        uri: `${config.mPlatfom}/${senderPsid}`,
        qs: {
          access_token: config.pageAccesToken,
          fields: "first_name, last_name, gender, locale, timezone"
        },
        method: "GET"
      })
        .on("response", function(response) {
          // console.log(response.statusCode);

          if (response.statusCode !== 200) {
            reject(Error(response.statusCode));
          }
        })
        .on("data", function(chunk) {
          body.push(chunk);
        })
        .on("error", function(error) {
          console.error("Unable to fetch profile:" + error);
          reject(Error("Network Error"));
        })
        .on("end", () => {
          body = Buffer.concat(body).toString();
          // console.log(JSON.parse(body));

          resolve(JSON.parse(body));
        });
    });
  }