A
A
Anastasia2021-03-06 16:18:56
Node.js
Anastasia, 2021-03-06 16:18:56

How to write a news suggestion bot?

Hello everyone, I decided to write a bot for my telegram channel, with which users can offer news, and these news will be sent to me:

1. The user clicks on one of the four buttons:

bot.on("message", (msg) => {
  const { id } = msg.chat;
  bot.sendMessage(
    id,
    "Привет, " +
      msg.from.first_name +
      ". Я бот для предложки новостей",
    options
  );
});

var options = {
  reply_markup: {
    inline_keyboard: [
      [{ text: "Предложить новость 1", callback_data: "news1" }],
      [{ text: "Предложить новость 2", callback_data: "news2" }],
    ],
  },
};


2. Depending on the pressed button, the user receives a response:

bot.on("callback_query", function onCallbackQuery(callbackQuery) {
  const action = callbackQuery.data;
  const msg = callbackQuery.message;
  const opts = {
    chat_id: msg.chat.id,
    message_id: msg.message_id,
  };
  let text;

  if (action === "news1") {
    text =
      "Вы можете написать ваш вопрос который касается страны";
  }
  if (action === "news2") {
    text =
      "Вы можете написать ваш вопрос который касается города";
  }
  }

  bot.editMessageText(text, opts);
});


And after that, I'm stuck and can't find a solution for my further ideas:

1. Along with the bot's response (text), I want a "Back" button that would return to the first item (choosing what kind of news to offer).
2. After the user writes the news, I want to show him a message that this news will be published. And the text of the news was sent to me.

I will be grateful for advice or links to resources where a similar problem is solved.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex K, 2021-03-06
@alexk111

1. Along with the bot's response (text), I want to have a "Back" button that would return to the first item (choosing what kind of news to offer).

When editing a message (editMessageText) in the options, you can also pass the keyboard markup reply_markup with the "Back" button. Accordingly, in the callback_query handler, you will also need to add a message substitution for the previous one if the "Back" button was pressed.
2. After the user writes the news, I want to show him a message that this news will be published. And the text of the news was sent to me.

To do this, you need to store the state of the user (the step on which he is). To check whether the user is at the step of entering a question when receiving a message, and perform the necessary actions with the entered question.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question