2022-03-10 06:59:48 +00:00
|
|
|
import { QueryError, QueryResult, QueryService } from '@tooljet-plugins/common';
|
2021-12-20 06:19:00 +00:00
|
|
|
import { Twilio } from 'twilio';
|
2022-03-10 06:59:48 +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') {
|
2023-06-15 12:03:22 +00:00
|
|
|
result = await this.getClient(sourceOptions.account_sid, sourceOptions.auth_token)
|
2021-12-20 06:19:00 +00:00
|
|
|
.messages.create({
|
|
|
|
|
body: queryOptions.body,
|
2023-06-15 12:03:22 +00:00
|
|
|
messagingServiceSid: sourceOptions.messaging_service_sid,
|
|
|
|
|
to: queryOptions.to_number,
|
2021-12-20 06:19:00 +00:00
|
|
|
})
|
|
|
|
|
.then((message) => message);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error.response);
|
|
|
|
|
throw new QueryError('Query could not be completed', error.message, {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
status: 'ok',
|
|
|
|
|
data: result,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|