UNPKG

@great-detail/whatsapp

Version:

SDK for interfacing with WhatsApp Business Platform in Typescript or Node.js using the Cloud API, hosted by Meta.

2 lines (1 loc) 6.3 kB
"use strict";var L=Object.create;var M=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var D=Object.getOwnPropertyNames;var K=Object.getPrototypeOf,A=Object.prototype.hasOwnProperty;var F=(s,e)=>{for(var t in e)M(s,t,{get:e[t],enumerable:!0})},j=(s,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of D(e))!A.call(s,r)&&r!==t&&M(s,r,{get:()=>e[r],enumerable:!(o=W(e,r))||o.enumerable});return s};var y=(s,e,t)=>(t=s!=null?L(K(s)):{},j(e||!s||!s.__esModule?M(t,"default",{value:s,enumerable:!0}):t,s)),T=s=>j(M({},"__esModule",{value:!0}),s);var H={};F(H,{Client:()=>d,CloudAPI:()=>d,MessageType:()=>U,default:()=>d});module.exports=T(H);var w=y(require("ky"),1),g=class{constructor(e){this._request=e}getEndpoint(e){return encodeURIComponent(e)+"/whatsapp_business_profile"}getBusinessProfile({phoneNumberID:e,fields:t,request:o}){return w.default.create({...this._request,method:"GET",searchParams:{fields:Object.keys(t??{}).join(",")}})(this.getEndpoint(e),o)}updateBusinessProfile({phoneNumberID:e,request:t,...o}){return w.default.create({...this._request,method:"POST",json:{messaging_product:"whatsapp",...o}})(this.getEndpoint(e),t)}};var O=y(require("ky"),1),P=class{constructor(e){this._request=e}getEndpoint(e){return encodeURIComponent(e)+"/media"}upload({phoneNumberID:e,file:t,filename:o,mimeType:r,request:f}){let a=new FormData;return a.set("messaging_product","whatsapp"),a.set("file",t,o),a.set("type",r),O.default.create({...this._request,method:"POST",body:a})(this.getEndpoint(e),f)}getURL({mediaID:e,phoneNumberID:t,request:o}){return O.default.create({...this._request,method:"GET",headers:{"Content-Type":"application/json"},searchParams:{...t?{phone_number_id:t}:{}}})(encodeURIComponent(e),o)}delete({mediaID:e,phoneNumberID:t,request:o}){return O.default.create({...this._request,method:"DELETE",headers:{"Content-Type":"application/json"},searchParams:{...t?{phone_number_id:t}:{}}})(encodeURIComponent(e),o)}download({mediaURL:e,request:t}){return O.default.create({method:"GET",headers:{"User-Agent":"node",Host:"lookaside.fbsbx.com"}})(e,t)}};var N=y(require("ky"),1),x=class{constructor(e){this._request=e}getEndpoint(e){return encodeURIComponent(e)+"/messages"}createStatus({phoneNumberID:e,request:t,...o}){return N.default.create({...this._request,method:"POST",json:{messaging_product:"whatsapp",...o}})(this.getEndpoint(e),t)}createMessage({to:e,phoneNumberID:t,context:o,recipientType:r="individual",request:f,...a}){return N.default.create({...this._request,method:"POST",json:{messaging_product:"whatsapp",recipient_type:r,context:o,to:e,...a}})(this.getEndpoint(t),f)}};var C=y(require("ky"),1),k=class{constructor(e){this._request=e}getEndpoint(e){return encodeURIComponent(e)+"/phone_numbers"}getPhoneNumber({phoneNumberID:e,fields:t,request:o}){return C.default.create({...this._request,method:"GET",searchParams:{fields:Object.keys(t??{}).join(",")}})(encodeURIComponent(e),o)}listPhoneNumbers({businessAccountID:e,sort:t,filtering:o,request:r}){return C.default.create({...this._request,method:"GET",searchParams:{...t?{sort:t}:{},...o?{filtering:o}:{}}})(this.getEndpoint(e),r)}};var R=y(require("ky"),1),S=class{constructor(e){this._request=e}getEndpoint(e){return encodeURIComponent(e)+"/subscribed_apps"}createSubscription({businessAccountID:e,request:t}){return R.default.create({...this._request,method:"POST"})(this.getEndpoint(e),t)}listSubscriptions({businessAccountID:e,request:t}){return R.default.create({...this._request,method:"GET"})(this.getEndpoint(e),t)}};var v=require("crypto");var i=class extends Error{};var p=class extends i{};var c=class extends i{};var m=class extends i{};var u=class extends i{};var h=class extends i{};var l=class extends i{};var q=class{errors={WebhookError:i,IncorrectMethodWebhookError:p,InvalidHubChallengeWebhookError:c,InvalidHubModeWebhookError:m,InvalidHubSignatureWebhookError:u,InvalidHubVerifyTokenWebhookError:h,MissingBodyWebhookError:l};async register(e){if(e.method.toLowerCase()!=="get")throw new p("Webhook Registration Requests must use the GET request method.");let t=e.query["hub.mode"]??void 0;if(!t||t!=="subscribe")throw new m("Webhook Registration Request must have query parameter: hub.mode=subscribe");let o=e.query["hub.challenge"]??void 0;if(!o)throw new c("Webhook Registration Request must have query parameter: hub.challenge");let r=e.query["hub.verify_token"]??void 0;if(!r)throw new h("Webhook Registration Request must have query parameter: hub.verify_token");return{verifyToken:r,challenge:o,accept:()=>o,reject:()=>{}}}async eventNotification(e){if(e.method.toLowerCase()!=="post")throw new p("Webhook Event Notification Request must use the POST request method.");let t=e.headers["x-hub-signature"]?.toString().replace("sha1=",""),o=e.headers["x-hub-signature-256"]?.toString().replace("sha256=","");if(!o)throw new u("Webhook Event Notification Request must have header: x-hub-signature-256");if(!e.body)throw new l("Webhook Event Notification Request must have a body");let r=e.body,f=JSON.parse(r),a=b=>_=>(0,v.createHmac)(b,_).update(r,"utf8").digest("hex"),E=(b,_)=>{let G=a(b);return I=>{let B=G(I);return _===B}};return{eventNotification:f,signature:{sha1:{value:t,getCalculatedSignature:a("sha1"),check:E("sha1",t)},sha256:{value:o,getCalculatedSignature:a("sha256"),check:E("sha256",o)}},checkSignature:E("sha256",o),verifySignature(b){if(!this.checkSignature(b))throw new u("Webhook Event Notification Signature doesn't match received body")},accept:()=>{}}}};var d=class{_request;businessProfile;message;phoneNumbers;subscribedApps;media;webhook;constructor({prefixUrl:e="https://graph.facebook.com",graphVersion:t="v20.0"}={}){this._request={prefixUrl:e.replace(/\/$/,"")+"/"+t},this.businessProfile=new g(this._request),this.message=new x(this._request),this.phoneNumbers=new k(this._request),this.subscribedApps=new S(this._request),this.media=new P(this._request),this.webhook=new q}};var U=(n=>(n.Audio="audio",n.Contacts="contacts",n.Document="document",n.Image="image",n.Interactive="interactive",n.Location="location",n.Reaction="sticker",n.Sticker="sticker",n.System="system",n.Button="button",n.Order="order",n.Template="template",n.Text="text",n.Video="video",n))(U||{});0&&(module.exports={Client,CloudAPI,MessageType});