Skip to content

Example Code in README doesn't seem to work #80

Description

@juni-vogt

Hi, I adapted the code from the README slightly to get a working example but I get errors...

The first message being sent/received works. But for the second message, I get an error. The server also gets killed after like two seconds. Could you help me with this?

I get this output

error: [polling_error] {}
    1) should greet Masha and Sasha


  0 passing (845ms)
  1 failing

  1) Telegram bot test
       should greet Masha and Sasha:
     Error: Timeout of 800ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/matthias/Code/subroulette_bot/test5.js)
      at listOnTimeout (node:internal/timers:557:17)
      at processTimers (node:internal/timers:500:7)


error: [polling_error] {"code":"EFATAL","message":"EFATAL: Error: connect ECONNREFUSED ::1:9001"}
error: [polling_error] {"code":"EFATAL","message":"EFATAL: Error: connect ECONNREFUSED ::1:9001"}
error: [polling_error] {"code":"EFATAL","message":"EFATAL: Error: connect ECONNREFUSED ::1:9001"}
error: [polling_error] {"code":"EFATAL","message":"EFATAL: Error: connect ECONNREFUSED ::1:9001"}
…

for mocha test.js where test.js is:

const TelegramServer = require('telegram-test-api');
const TelegramBot = require('node-telegram-bot-api');

class TestBot {
  constructor(bot) {
    bot.onText(/\/start/, (msg, match) => {
      let chatId = msg.from.id;
      let opts = {
        reply_to_message_id: msg.message_id,
        reply_markup: JSON.stringify({
          keyboard: [[{text: 'ok 1'}]],
        }),
      };
      bot.sendMessage(chatId, 'pong', opts);
    });
    bot.onText(/ok 1/, (msg, match) => {
      let chatId = msg.from.id;
      bot.sendMessage(chatId, 'Hello, Masha!', opts);
    });
  }
}


describe('Telegram bot test', () => {
  let serverConfig = {port: 9001};
  const token = 'sampleToken';
  let server;
  let client;
  beforeEach(() => {
    server = new TelegramServer(serverConfig);
    return server.start().then(() => {
      client = server.getClient(token);
    });
  });

  afterEach(function () {
    this.slow(2000);
    this.timeout(10000);
    return server.stop();
  });

  it('should greet Masha and Sasha', async function testFull() {
    this.slow(400);
    this.timeout(800);
    const message = client.makeMessage('/start');
    await client.sendMessage(message);
    const botOptions = {polling: true, baseApiUrl: server.config.apiURL};
    const telegramBot = new TelegramBot(token, botOptions);
    const testBot = new TestBot(telegramBot);
    const updates = await client.getUpdates();
    console.log(`Client received messages: ${JSON.stringify(updates.result)}`);
    if (updates.result.length !== 1) {
      throw new Error('updates queue should contain one message!');
    }

    let keyboard = updates.result[0].message.reply_markup.keyboard;
    console.log("keyboard", keyboard)

    const message2 = client.makeMessage(keyboard[0][0].text);
    await client.sendMessage(message2);
    console.log("sent message2", message2)

    // Here, the error occurs:
    const updates2 = await client.getUpdates();
    console.log(`Client received messages: ${JSON.stringify(updates2.result)}`);


    if (updates2.result.length !== 1) {
      throw new Error('updates queue should contain one message!');
    }
    if (updates2.result[0].message.text !== 'Hello, Masha!') {
      throw new Error('Wrong greeting message!');
    }
    
    return true;
  });
});

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions