Added MessageInterface and fixed Types for Send message

This commit is contained in:
huzaif 2020-11-16 21:33:46 +05:30
parent 05fa8ba80d
commit 8f161d1bcb
4 changed files with 100 additions and 65 deletions

View file

@ -1,17 +1,32 @@
import * as _ from "lodash";
import {
PlivoGenericResponse,
PlivoResource,
PlivoResourceInterface
} from '../base';
import {
extend,
validate
} from '../utils/common.js';
import {
PlivoResource,
PlivoResourceInterface
} from '../base';
import * as _ from "lodash";
const clientKey = Symbol();
const action = 'Message/';
const idField = 'messageUuid';
let actionKey = Symbol('api action');
let klassKey = Symbol('constructor');
let idKey = Symbol('id filed');
let clientKey = Symbol('make api call');
export class MessageResponse {
constructor(params) {
params = params || {};
this.apiId= params.apiId;
this.message = params.message;
this.messageUuid = params.messageUuid;
}
}
/**
* Represents a Message
* @constructor
@ -47,6 +62,10 @@ export class MessageInterface extends PlivoResourceInterface {
super(action, Message, idField, client);
extend(this, data);
this[clientKey] = client;
this[actionKey] = action;
this[klassKey] = Message;
this[idKey] = idField;
}
/**
@ -69,7 +88,7 @@ export class MessageInterface extends PlivoResourceInterface {
}
/**
* Send Message
* Create Message
* @method
* @param {string} src - source number
* @param {string} dst - destination number
@ -80,10 +99,10 @@ export class MessageInterface extends PlivoResourceInterface {
* @param {string} [optionalParams.method] The method used to call the url. Defaults to POST.
* @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true.
* @param {Array} [optionalParams.media_urls] For sending mms, specify the media urls in list of string
* @promise {object} return {@link PlivoGenericMessage} object if success
* @promise {object} return {@link MessageResponse} object if success
* @fail {Error} return Error
*/
create(src, dst, text, optionalParams, powerpackUUID) {
createtest(src, dst, text, optionalParams, powerpackUUID) {
let errors = validate([{
field: 'dst',
value: dst,
@ -117,7 +136,20 @@ export class MessageInterface extends PlivoResourceInterface {
if (powerpackUUID) {
params.powerpackUUID = powerpackUUID;
}
return super.create(params);
let client = this[clientKey];
let idField = this[idKey];
let action = this[actionKey] + (this.id ? this.id + '/' : '');
return new Promise((resolve, reject) => {
client('POST', action, params)
.then(response => {
resolve(new MessageResponse(response.body, idField));
})
.catch(error => {
reject(error);
});
})
}
/**

View file

@ -2,21 +2,23 @@
const utf8 = require('utf8');
const buildUrl = require('build-url');
const base64 = require('base-64');
import * as parser from 'uri-parser';
import crypto from 'crypto';
import _ from 'lodash';
export function computeOldSignature(authId: string, uri: string, params: {[string]: string}): string {
import * as parser from 'uri-parser';
import _ from 'lodash';
import crypto from 'crypto';
export function computeOldSignature(authId, uri, params){
const joinedParams = uri + _.join(_.map(_.sortBy(_.toPairs(params)), item => item[0] + item[1]), '');
// console.log(joinedParams);
return crypto.createHmac('sha1', authId).update(joinedParams).digest('base64');
}
export function verifyOldSignature(authId: string, uri: string, params: {[string]: string}, signature: string): boolean {
export function verifyOldSignature(authId, uri, params, signature) {
return computeOldSignature(authId, uri, params) === signature;
}
export function validateSignature(uri: string, nonce: string, signature: string, auth_token: string) {
export function validateSignature(uri, nonce, signature, auth_token) {
nonce = utf8.encode(nonce);
signature = utf8.encode(signature);
auth_token = utf8.encode(auth_token);

View file

@ -3,9 +3,11 @@ const utf8 = require('utf8');
const buildUrl = require('build-url');
const base64 = require('base-64');
const qs = require('querystring');
import * as parser from 'uri-parser';
import crypto from 'crypto';
import _ from 'lodash';
import crypto from 'crypto';
function get_map_from_query(params1, params2) {
let params = {};
@ -85,9 +87,9 @@ function get_signature_v3(auth_token, base_url, nonce) {
return base64.encode(hmacBytes);
}
export function validateV3Signature(method: string, uri: string,
nonce: string, auth_token: string,
v3_signature: string, params={}) {
export function validateV3Signature(method, uri,
nonce, auth_token,
v3_signature, params={}) {
auth_token = utf8.encode(auth_token);
nonce = utf8.encode(nonce);
v3_signature = utf8.encode(v3_signature);

View file

@ -1,4 +1,12 @@
import { PlivoResource, PlivoResourceInterface } from '../base';
export declare class MessageResponse {
constructor(params: object);
apiId: string
message: string
messageUuid: object
}
/**
* Represents a Message
* @constructor
@ -6,8 +14,8 @@ import { PlivoResource, PlivoResourceInterface } from '../base';
* @param {object} [data] - data of call
*/
export declare class Message extends PlivoResource {
constructor(client: any, data?: {});
listMedia(): any;
constructor(client: any, data?: {});
listMedia(): any;
}
/**
* Represents a Message Interface
@ -16,46 +24,37 @@ export declare class Message extends PlivoResource {
* @param {object} [data] - data of call
*/
export declare class MessageInterface extends PlivoResourceInterface {
constructor(client: any, data?: {});
/**
* Send Message
* @method
* @param {string} src - source number
* @param {string} dst - destination number
* @param {string} text - text to send
* @param {object} optionalParams - Optional Params to send message
* @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`.
* @param {string} [optionalParams.url] The URL to which with the status of the message is sent.
* @param {string} [optionalParams.method] The method used to call the url. Defaults to POST.
* @param {list} [optionalParams.media_urls] For sending mms, specify the media urls in list of string
* @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true.
* @promise {object} return {@link PlivoGenericMessage} object if success
* @fail {Error} return Error
*/
send(src: any, dst: any, text: any, optionalParams: any): any;
/**
* Create Message
* @method
* @param {string} src - source number
* @param {string} dst - destination number
* @param {string} text - text to send
* @param {object} optionalParams - Optional Params to send message
* @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`.
* @param {string} [optionalParams.url] The URL to which with the status of the message is sent.
* @param {string} [optionalParams.method] The method used to call the url. Defaults to POST.
* @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true.
* @param {Array} [optionalParams.media_urls] For sending mms, specify the media urls in list of string
* @promise {object} return {@link PlivoGenericMessage} object if success
* @fail {Error} return Error
*/
create(src: any, dst: any, text: any, optionalParams?: any, powerpackUUID: any): any;
/**
* Get Message by given id
* @method
* @param {string} id - id of message
* @promise {object} return {@link Message} object if success
* @fail {Error} return Error
*/
get(id: any): any;
listMedia(messageUUID: any): any;
}
constructor(client: any, data?: {});
/**
* Send Message
* @method
* @param {string} src - source number
* @param {string} dst - destination number
* @param {string} text - text to send
* @param {object} optionalParams - Optional Params to send message
* @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`.
* @param {string} [optionalParams.url] The URL to which with the status of the message is sent.
* @param {string} [optionalParams.method] The method used to call the url. Defaults to POST.
* @param {list} [optionalParams.media_urls] For sending mms, specify the media urls in list of string
* @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true.
* @promise {object} return {@link PlivoGenericMessage} object if success
* @fail {Error} return Error
*/
send(src: any, dst: string, text: string, optionalParams: object): Promise<MessageResponse>;
/**
* Create Message
* @method
* @param {string} src - source number
* @param {string} dst - destination number
* @param {string} text - text to send
* @param {object} optionalParams - Optional Params to send message
* @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`.
* @param {string} [optionalParams.url] The URL to which with the status of the message is sent.
* @param {string} [optionalParams.method] The method used to call the url. Defaults to POST.
* @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true.
* @param {Array} [optionalParams.media_urls] For sending mms, specify the media urls in list of string
* @promise {object} return {@link MessageResponse} object if success
* @fail {Error} return Error
*/
createtest(src: any, dst: string, text: string, optionalParams: object, powerpackUUID: any): Promise<MessageResponse>;
}