@uneiotech/elisa_whatsapp
Version:
Elisa WhatsApp is a library to stable a communication between channels and server.
64 lines (57 loc) • 1.85 kB
text/typescript
import { IConfig, IMessage, IBodyMaytapi, IResponse } from "./types";
import * as request from 'request-promise'
export class ElisaWhatsApp{
private config:IConfig
private authorizationHeaders = {
"x-maytapi-key":""
}
constructor(config:IConfig){
this.config = config
// set authorization key
this.authorizationHeaders["x-maytapi-key"] = config.apiKey
}
public async sendMessage(message:IMessage):Promise<IResponse>{
let bodyOfMessage:IBodyMaytapi = {to_number:"",type:"",message:""}
if(message.type === "text"){
bodyOfMessage = {
"to_number":message.numberTo,
"type":message.type,
"message":message.message
}
}
if(message.type === "link"){
bodyOfMessage = {
"to_number":message.numberTo,
"type" :message.type,
"message" :message.link,
"text" :message.message
}
}
return await this.callService(bodyOfMessage)
}
private callService(body:IBodyMaytapi):Promise<IResponse>{
return new Promise((resolve,reject) =>{
request.post(
{
url:this.config.url,
headers: this.authorizationHeaders,
json: true,
body
}
).then((res:any) =>{
resolve({
ok:res.success,
status:200,
message:res.message
})
})
.catch((err:any) =>{
reject({
ok:false,
status:err.statusCode,
message:err.message
})
})
});
}
}