Process response from telegram bot api
I am using this api: https://github.com/orzFly/node-telegram-bot
It should work like everyone else.
Now I want my Bot to be able to update the string it is holding for some reason. so "/ update" the update function is called, where msg is the message object ( https://core.telegram.org/bots/api#message ):
link = "something";
function update(msg) {
response = tg.sendMessage({
text: "Send a new URL, please",
chat_id: msg.chat.id,
reply_to_message_id: msg.message_id,
reply_markup: {
force_reply: true,
selective: true
}
});
console.log("response: " + response);
// on reply I want to update link
}
Now this bot is asking me to provide a new line. The next response in the telegram is already a response to a bot request due to force_reply. How do I get this response? The "answer" here is the object of the promise, and I don't know what to do with it.
After reading the Promises objects, I tried something like this:
response.then(function successHandler(result) {
tg.sendMessage({
text: "new URL is: I don't know",
chat_id: msg.chat.id
});
}, function failureHandler(error) {
colsole.log("error: " + error);
});
But it didn't work. In no case.
I just don't know where to get the response message object from. Hope it's clear what I'm asking. If not, let me know.
source to share
If I understand correctly, you are trying to get the following message from the user and treat it as a new line; The problem is as follows: the response will contain a response from Telegram servers, which indicates the result of the message you tried to send; it has nothing to do with the user's response to your message;
To do this, you need to control what last message your bot sent to the user, and, based on that, decide how to handle that next message user; it might look something like this:
link = "something";
states = {}
function update(msg) {
if (!states[msg.chat.id] || states[msg.chat.id] == 1) {
tg.sendMessage({
text: "Send a new URL, please",
chat_id: msg.chat.id,
reply_to_message_id: msg.message_id,
reply_markup: {
force_reply: true,
selective: true
}
}).then(() => {
states[msg.chat.id] = 2
console.log(`Asked a question to ${msg.chat.id}`);
});
} else {
link = msg.text;
tg.sendMessage({
text: `New URL is: ${link}`,
chat_id: msg.chat.id,
reply_to_message_id: msg.message_id
})
}
}
source to share
It looks like the result of the promise is the whole Telegram response. So your result will be inresult.result.text
The variable result
will look like this:
{
ok: true
result: {
message_id: x,
from: { ... }
chat: { ... }
date: x,
text: 'message'
}
}
This is sad, I would assume the author only returns result
.
var api = require('telegram-bot');
api = new api(<TOKEN>);
api.sendMessage({ chat_id: 0, text: 'test' }).then(function (result) {
console.log(result);
});
api.on('message', function (msg) {
console.log(msg); // <- will contain the reply
// msg.text
// msg.chat.id
// msg.from.id
});
api.start();
source to share