2022-01-24 13:59:21 +00:00
|
|
|
import { QueryError, QueryResult, QueryService } from '@tooljet-plugins/common';
|
2021-12-20 06:19:00 +00:00
|
|
|
import { Twilio } from 'twilio';
|
2022-01-24 13:59:21 +00:00
|
|
|
import { SourceOptions, QueryOptions } from './types'
|
2021-12-20 06:19:00 +00:00
|
|
|
|
|
|
|
|
export default class TwilioQueryService implements QueryService {
|
|
|
|
|
getClient(accountSid: string, authToken: string): any {
|
|
|
|
|
return new Twilio(accountSid, authToken);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 13:59:21 +00:00
|
|
|
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
|
2021-12-20 06:19:00 +00:00
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|