Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('reject when telegram return not success', async () => {
const ctx = setup({ webhook: 'http://example.com/webhook' });
TelegramClient.connect().setWebhook.mockImplementation(() => {
throw new Error();
});
await setWebhook(ctx);
expect(log.error).toBeCalledWith('Failed to set Telegram webhook');
expect(process.exit).toBeCalled();
});
});
it('reject when telegram return not success', async () => {
const ctx = setup({ webhook: 'http://example.com/webhook' });
TelegramClient.connect().setWebhook.mockImplementation(() => {
throw new Error();
});
await setWebhook(ctx);
expect(log.error).toBeCalledWith('Failed to set Telegram webhook');
expect(process.exit).toBeCalled();
});
});
it('reject when Telegram return not success', () => {
const ctx = {
argv: {},
};
TelegramClient.connect().getWebhookInfo.mockResolvedValueOnce({
ok: false,
});
expect(getWebhook(ctx).then).toThrow();
});
it('reject when Telegram return not success', () => {
const ctx = {
argv: {},
};
TelegramClient.connect().deleteWebhook.mockResolvedValueOnce({
ok: false,
});
expect(deleteWebhook(ctx).then).toThrow();
});
export async function getWebhook(_: CliContext): Promise {
try {
const config = getChannelConfig(Channel.Telegram);
const { accessToken } = config;
invariant(
accessToken,
'`accessToken` is not found in the `bottender.config.js` file'
);
const client = TelegramClient.connect({
accessToken,
});
const result = await client.getWebhookInfo();
Object.entries(result).forEach(([key, value]) => print(`${key}: ${value}`));
} catch (err) {
error('Failed to get Telegram webhook');
if (err.response) {
error(`status: ${bold(err.response.status)}`);
if (err.response.data) {
error(`data: ${bold(JSON.stringify(err.response.data, null, 2))}`);
}
} else {
error(err.message);
export async function deleteWebhook(_: CliContext): Promise {
try {
const config = getChannelConfig(Channel.Telegram);
const { accessToken } = config;
invariant(
accessToken,
'`accessToken` is not found in the `bottender.config.js` file'
);
const client = TelegramClient.connect({
accessToken,
});
await client.deleteWebhook();
print('Successfully delete Telegram webhook');
} catch (err) {
error('Failed to delete Telegram webhook');
if (err.response) {
error(`status: ${bold(err.response.status)}`);
if (err.response.data) {
error(`data: ${bold(JSON.stringify(err.response.data, null, 2))}`);
}
} else {
error(err.message);
});
const ngrokPort = argv['--ngrok-port'] || '4040';
let webhook = argv['--webhook'];
try {
const config = getChannelConfig(Channel.Telegram);
const { accessToken, path = '/webhooks/telegram' } = config;
invariant(
accessToken,
'`accessToken` is not found in the `bottender.config.js` file'
);
const client = TelegramClient.connect({
accessToken,
});
if (!webhook) {
warn('We can not find the webhook callback url you provided.');
const prompt = new Confirm(
`Are you using ngrok (get url from ngrok server on http://127.0.0.1:${ngrokPort})?`
);
const result = await prompt.run();
if (result) {
webhook = `${await getWebhookFromNgrok(ngrokPort)}${path}`;
}
}
invariant(
webhook,
constructor(options: ConstructorOptions) {
if ('client' in options) {
this._client = options.client;
} else {
const { accessToken, origin } = options;
this._client = TelegramClient.connect({
accessToken,
origin,
});
}
}