How to use messaging-api-telegram - 10 common examples

To help you get started, we’ve selected a few messaging-api-telegram examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Yoctol / bottender / packages / bottender / src / telegram / __tests__ / TelegramContext.spec.ts View on Github external
beforeEach(() => {
  /* eslint-disable global-require */
  TelegramClient = require('messaging-api-telegram').TelegramClient;
  TelegramContext = require('../TelegramContext').default;
  TelegramEvent = require('../TelegramEvent').default;
  sleep = require('delay');
  warning = require('warning');
  /* eslint-enable global-require */
});
github Yoctol / bottender / packages / bottender / src / cli / providers / telegram / __tests__ / setWebhook.spec.ts View on Github external
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();
  });
});
github Yoctol / bottender / packages / bottender / src / cli / providers / telegram / __tests__ / setWebhook.spec.ts View on Github external
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();
  });
});
github Yoctol / bottender / packages / bottender / src / cli / providers / telegram / __tests__ / getWebhook.spec.ts View on Github external
it('reject when Telegram return not success', () => {
    const ctx = {
      argv: {},
    };
    TelegramClient.connect().getWebhookInfo.mockResolvedValueOnce({
      ok: false,
    });

    expect(getWebhook(ctx).then).toThrow();
  });
github Yoctol / bottender / packages / bottender / src / cli / providers / telegram / __tests__ / deleteWebhook.spec.ts View on Github external
it('reject when Telegram return not success', () => {
    const ctx = {
      argv: {},
    };

    TelegramClient.connect().deleteWebhook.mockResolvedValueOnce({
      ok: false,
    });

    expect(deleteWebhook(ctx).then).toThrow();
  });
github Yoctol / bottender / packages / bottender / src / cli / providers / telegram / webhook.ts View on Github external
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);
github Yoctol / bottender / packages / bottender / src / cli / providers / telegram / webhook.ts View on Github external
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);
github Yoctol / bottender / packages / bottender / src / cli / providers / telegram / webhook.ts View on Github external
});

  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,
github Yoctol / bottender / packages / bottender / src / telegram / TelegramConnector.ts View on Github external
constructor(options: ConstructorOptions) {
    if ('client' in options) {
      this._client = options.client;
    } else {
      const { accessToken, origin } = options;

      this._client = TelegramClient.connect({
        accessToken,
        origin,
      });
    }
  }

messaging-api-telegram

Messaging API client for Telegram

MIT
Latest version published 3 years ago

Package Health Score

51 / 100
Full package analysis