Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const provider = providers[providerName];
if (argv['--help']) {
provider.help();
process.exit(0);
}
// the context object to supply to the providers or the commands
const ctx: CliContext = {
config: null, // FIXME
argv,
};
try {
const method = get(provider, camelcase(subcommand));
if (method) {
await (provider as any)[camelcase(subcommand)](ctx);
} else {
const subcommands = Array.from(provider.subcommands).join(', ');
error(`Please specify a valid subcommand: ${subcommands}`);
provider.help();
}
} catch (err) {
console.error(
error(
`An unexpected error occurred in provider ${subcommand}: ${err.stack}`
)
);
}
};
if (argv['--help']) {
provider.help();
process.exit(0);
}
// the context object to supply to the providers or the commands
const ctx: CliContext = {
config: null, // FIXME
argv,
};
try {
const method = get(provider, camelcase(subcommand));
if (method) {
await (provider as any)[camelcase(subcommand)](ctx);
} else {
const subcommands = Array.from(provider.subcommands).join(', ');
error(`Please specify a valid subcommand: ${subcommands}`);
provider.help();
}
} catch (err) {
console.error(
error(
`An unexpected error occurred in provider ${subcommand}: ${err.stack}`
)
);
}
};
options: { quickReplies?: Types.QuickReply[] } = {}
): FormData {
const message: Types.FileDataMediaAttachmentMessage = {
...payload,
};
// snakecase support for backward compatibility
const quickReplies = options.quickReplies || (options as any).quick_replies;
if (quickReplies && Array.isArray(quickReplies) && quickReplies.length >= 1) {
message.quickReplies = quickReplies;
}
const formdata = new FormData();
formdata.append('message', JSON.stringify(snakecaseKeysDeep(message)));
formdata.append(
'filedata',
filedata,
// FIXME: use pick for formdata options
omit(options, ['quickReplies'])
);
return formdata;
}
function createMessage(
payload: Types.Message,
options: { quickReplies?: Types.QuickReply[] } = {}
): Types.Message {
const message = {
...payload,
};
// snakecase support for backward compatibility
const quickReplies = options.quickReplies || (options as any).quick_replies;
if (quickReplies && Array.isArray(quickReplies) && quickReplies.length >= 1) {
message.quickReplies = quickReplies;
}
return camelcaseKeysDeep(message);
}
async _callAPI(
path: string,
body: Record = {}
): Promise | never> {
try {
const response = await this._axios.post(
path,
// we can't apply a deep snake_case transform here
// because it accept only PascalCase for keyboard and rich media
snakecaseKeys(body, { deep: false })
);
const { config, request } = response;
const data = (camelcaseKeysDeep(
response.data
) as any) as Types.ResponseData;
if (data.status !== 0) {
throw new AxiosError(`Viber API - ${data.statusMessage}`, {
config,
request,
response,
});
}
return data;
} catch (err) {
throw new AxiosError(err.message, err);
}
}
const appSecretProof = crypto
.createHmac('sha256', appSecret)
.update(accessToken, 'utf8')
.digest('hex');
// eslint-disable-next-line no-param-reassign
config.url = appendQuery(config.url || '', {
appsecret_proof: appSecretProof,
});
return config;
});
}
this._axios.interceptors.request.use(
createRequestInterceptor({ onRequest: this._onRequest })
);
}
this._token = config.accessToken;
this._onRequest = config.onRequest;
origin = config.origin;
}
// Web API
// https://api.slack.com/web
this._axios = axios.create({
baseURL: `${origin || 'https://slack.com'}/api/`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
this._axios.interceptors.request.use(
createRequestInterceptor({ onRequest: this._onRequest })
);
this.chat = {
postMessage: this._postMessage.bind(this),
postEphemeral: this._postEphemeral.bind(this),
update: this._updateMessage.bind(this),
delete: this._deleteMessage.bind(this),
meMessage: this._meMessage.bind(this),
getPermalink: this._getPermalink.bind(this),
scheduleMessage: this._scheduleMessage.bind(this),
deleteScheduledMessage: this._deleteScheduledMessage.bind(this),
unfurl: this._unfurl.bind(this),
scheduledMessages: {
list: this._getScheduledMessages.bind(this),
},
};
url = config.url;
this._onRequest = config.onRequest;
} else {
url = urlOrConfig;
}
// incoming webhooks
// https://api.slack.com/incoming-webhooks
this._axios = axios.create({
baseURL: url,
headers: { 'Content-Type': 'application/json' },
});
this._axios.interceptors.request.use(
createRequestInterceptor({ onRequest: this._onRequest })
);
}
async _callAPI(
path: string,
body: Record = {}
): Promise | never> {
try {
const response = await this._axios.post(
path,
// we can't apply a deep snake_case transform here
// because it accept only PascalCase for keyboard and rich media
snakecaseKeys(body, { deep: false })
);
const { config, request } = response;
const data = (camelcaseKeysDeep(
response.data
) as any) as Types.ResponseData;
if (data.status !== 0) {
throw new AxiosError(`Viber API - ${data.statusMessage}`, {
config,
request,
response,
});
}
.map(([channel, { path: webhookPath, ...channelConfig }]) => {
// eslint-disable-next-line import/no-dynamic-require
const ChannelBot = require(`../${channel}/${pascalcase(channel)}Bot`)
.default;
const channelBot = new ChannelBot({
...channelConfig,
sessionStore,
}) as Bot;
initializeBot(channelBot);
return {
webhookPath: webhookPath || `/webhooks/${channel}`,
bot: channelBot,
};
});