Questions Module
The Questions module is designed to capture messages in Telegram and pass them to a callback function. This allows you to easily implement application logic based on user messages. This module is also used in MessageContext
in the question
method. The module is stored in an Evogram instance in the modules object (client.modules.questions
).
Working directly with the module is necessary when you need to request data outside of a command/event, where a UserMessageContext
is passed. If you have a UserMessageContext
, use the built-in question
method.
Methods
The QuestionManager
class has two main methods that are necessary for working with the module.
addQuestion(user_id, callback)
Adds a new question to the queue for the specified user and associates it with the passed callback
.
Accepts two parameters:
user_id
: The ID of the user who asked the question.callback
: The callback function that will be called when the user responds.
getQuestion(user_id, deleteQuestion)
Gets the callback function associated with the specified user and removes it from the queue if the deleteQuestion
parameter is set to true
.
Accepts two parameters:
user_id
: The ID of the user whose callback function should be obtained.deleteQuestion
: (Optional) A flag indicating whether to remove the question from the queue after receiving the callback. Default istrue
.
Usage example
Let's write code that will request a user's response and output it to the console.
client.modules.questions.addQuestion(1, (message) => {
console.log(message.text);
message.reply("Thank you for your answer!");
})
In this example, client
is an instance of Evogram
.