ToolJet/plugins/packages/twilioapi/lib/index.ts
Gandharv d367720c66
Add source options types for plugins (#1861)
* add source options types

* Remove query type

* fix source options

* add accesstoken - slack

* add types.ts file

* add idx files

* Add query options type for plugins (#1878)

* add query options type

* add missing import
2022-01-24 19:29:21 +05:30

33 lines
1.1 KiB
TypeScript

import { QueryError, QueryResult, QueryService } from '@tooljet-plugins/common';
import { Twilio } from 'twilio';
import { SourceOptions, QueryOptions } from './types'
export default class TwilioQueryService implements QueryService {
getClient(accountSid: string, authToken: string): any {
return new Twilio(accountSid, authToken);
}
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
let result = {};
try {
if (queryOptions.operation && queryOptions.operation === 'send_sms') {
result = await this.getClient(sourceOptions.accountSid, sourceOptions.authToken)
.messages.create({
body: queryOptions.body,
messagingServiceSid: sourceOptions.messagingServiceSid,
to: queryOptions.toNumber,
})
.then((message) => message);
}
} catch (error) {
console.log(error.response);
throw new QueryError('Query could not be completed', error.message, {});
}
return {
status: 'ok',
data: result,
};
}
}