Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
juni-vogt opened this issue Jun 4, 2023 · 3 comments
Open

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

juni-vogt opened this issue Jun 4, 2023 · 3 comments

Comments

@juni-vogt
Copy link

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;
  });
});
@juni-vogt
Copy link
Author

Personally, I am fine now because I now found the telegraf example which works, but leaving this open because it still doesn't make sense to me.

@Vishtar
Copy link

Vishtar commented May 1, 2024

@matthias-vogt, can you share the example?

@juni-vogt
Copy link
Author

Thanks for replying. It is the code above. I have since abandoned this project though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants