From c0c7e3452ab42aca19945da6fea5ec79da8ae1b5 Mon Sep 17 00:00:00 2001 From: cyberboysumanjay Date: Sat, 2 Oct 2021 20:03:53 +0530 Subject: [PATCH] Added Courier Tracker Plugin --- README.md | 2 ++ commands/courier.js | 65 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 commands/courier.js diff --git a/README.md b/README.md index 9ab2d59b..9c02938b 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ - **watch** - Find out where to watch a Movie/Show - **shorten** - Get Short URL - **ocr** - Extract Text from Image +- **courier** - Fetches latest status of Courier/Shipments from multiple providers [More modules]("https://github.com/TheWhatsBot/WhatsBot/wiki/Commands") @@ -64,6 +65,7 @@ [![Deploy with Heroku](https://www.herokucdn.com/deploy/button.svg "Deploy with Heroku")](https://heroku.com/deploy?template=https://github.com/TheWhatsBot/WhatsBot "Deploy with Heroku")
[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/new/template?template=https%3A%2F%2Fgithub.com%2FTheWhatsBot%2FWhatsBot&plugins=mongodb&envs=SESSION%2CPMPERMIT_ENABLED%2CDEFAULT_TR_LANG%2CENABLE_DELETE_ALERT%2CYT_DATA_API_KEY%2COCR_SPACE_API_KEY%2CINFOSPACE_API_KEY&optionalEnvs=YT_DATA_API_KEY%2COCR_SPACE_API_KEY%2CINFOSPACE_API_KEY&SESSIONDesc=Puppeteer+Session.+Ge+it+by+running+genToken.js&PMPERMIT_ENABLEDDesc=Enable+Pmpermit+write+true+or+false+only&DEFAULT_TR_LANGDesc=Default+Translation+Language&ENABLE_DELETE_ALERTDesc=If+true+and+if+someone+delete+message+in+PM%2C+Bot+will+send+the+deleted+message+in+that+chat+%28Exclude+Media%29&YT_DATA_API_KEYDesc=Youtube+DATA+API+key+grab+it+from+cloud.google.com&OCR_SPACE_API_KEYDesc=Get+it+from+https%3A%2F%2Focr.space%2FOCRAPI&INFOSPACE_API_KEYDesc=Get+it+from+https%3A%2F%2Finfospace.club&PMPERMIT_ENABLEDDefault=true&DEFAULT_TR_LANGDefault=en&referralCode=tuhin) + ### Commands : _Send !help in Anywhere. Or read this [Command's Wiki](https://github.com/TheWhatsBot/WhatsBot/wiki/Commands "Command's Wiki")_ diff --git a/commands/courier.js b/commands/courier.js new file mode 100644 index 00000000..505f23c5 --- /dev/null +++ b/commands/courier.js @@ -0,0 +1,65 @@ +//jshint esversion:8 +// Coded by Sumanjay (https://github.com/cyberboysumanjay) +const { + MessageMedia +} = require('whatsapp-web.js'); +const axios = require('axios'); + +async function getTrackingDetails(trackingService, trackingNumber) { + let mainconfig = { + method: 'get', + url: `https://sjcourierapi.deta.dev/${trackingService}/${trackingNumber}` //Feel free to use this API for your own use. + }; + console.log(mainconfig.url); + let statusString = "Unable to get information for your shipment. Please check the tracking id or try again later!" + return axios(mainconfig) + .then(async function (response) { + let data = response.data; + if (trackingService == "gati") { + let status = data.result + if (status == "successful") { + let dktInfo = data.dktInfo[0]; + console.log(dktInfo); + statusString = `Your shipment having docket number ${dktInfo.dktno} booked from ${dktInfo.bookingStation} to ${dktInfo.deliveryStation} by ${dktInfo.consigneeName} is having current status of ${dktInfo.docketStatus}.\nIt is scheduled to be delivered to ${dktInfo.ReceiversName} on or before ${dktInfo.assuredDlyDate}.` + } + } else if (trackingService == "ekart") { + try { + merchant = data.merchantName + if (merchant != null) { + let merchantName = merchant == "FKMP" ? "Flipkart" : merchant; + let reachedNearestHub = data.reachedNearestHub ? "has reached nearest hub" : "is yet to reach nearest hub" + statusString = `Your shipment having tracking number ${trackingNumber} booked from ${data.sourceCity} to ${data.destinationCity} by ${merchantName} ${reachedNearestHub}.\nYour shipment is expected to be delivered to ${data.receiverName} on or before ${data.expectedDeliveryDate}.` + } + } catch (error) { + console.log(error) + } + } + let out = ({ + status: statusString + }); + return out; + }) + .catch(function (error) { + console.log(error); + return "error"; + }); +} +const execute = async (client, msg, args) => { + msg.delete(true); + let data = await getTrackingDetails(args[0], args[1]); + if (data == "error") { + await client.sendMessage(msg.to, `🙇‍♂️ *Error*\n\n` + "```Something unexpected happened while fetching the courier details.```"); + } else { + await client.sendMessage(msg.to, `🙇‍♂️ *Courier/Shipment Details*\n\n` + "```" + data.status + "```"); + } +}; + +module.exports = { + name: 'Courier', + description: 'Get courier details from multiple providers. Currently supports: Gati Express and Ekart', + command: '!courier', + commandType: 'plugin', + isDependent: false, + help: `*courier*\n\nGet information about your couriers and shipments. \n\n*!courier [courier-name] [tracking-id]*\n`, + execute +}; \ No newline at end of file