我使用的方法如下:
class Bot {
constructor(token) {
let _baseApiURL = `https://api.telegram.org`;
//code here
}
getAPI(apiName) {
return axios.get(`${this.getApiURL()}/${apiName}`);
}
getApiURL() {
return `${this.getBaseApiUrl()}/bot${this.getToken()}`;
}
getUpdates(fn) {
this.getAPI('getUpdates')
.then(res => {
this.storeUpdates(res.data);
fn(res.data);
setTimeout(() => {
this.getUpdates(fn);
}, 1000);
})
.catch(err => {
console.log('::: ERROR :::', err);
});
}
}
const bot = new Bot('mytoken');
bot.start();
我想知道是否有更好的方法来监听 Telegram 的更新,而不是使用超时并重做对“getUpdates”API 的 Ajax 调用
最佳答案
Telegram 支持轮询或 Webhook,因此您可以使用后者来避免轮询 getUpdates
API
获取更新
There are two mutually exclusive ways of receiving updates for your bot — the getUpdates method on one hand and Webhooks on the other. Incoming updates are stored on the server until the bot receives them either way, but they will not be kept longer than 24 hours.
无论您选择哪个选项,您都会收到 JSON 序列化的 Update 对象。
<小时/>关于node.js - 有没有更好的方法使用 NodeJs 从 Telegram 机器人获取更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50275931/