我正在 telegram 上创建游戏,目前我遇到了同时处理多个更新的问题。我正在使用 Node.js
例如我有这个代码
var TelegramBot = require('node-telegram-bot-api'),
bot = new TelegramBot("MY_TOKEN", {polling: true});
bot.onText(/^\/createroom/, function (res, match) {
//Here i have some logic, to check whether if the room already created or not
service.checkIfRoomExist(res) // this service here, will always return false, because of the simultaneously chat
.then (function(isExist) {
if (isExist === false) {
service.createRoom(res)
.then (function() {
});
}
});
//it works fine, if player type "/createroom" not simultaneously
//but if more than 1 player type "/createroom" simultaneously, my logic here doesn't work, it will create multiple room
}
有什么想法可以解决这个问题吗?
非常感谢,任何帮助将不胜感激
最佳答案
您需要将唯一的聊天/用户 ID 链接到您的数据库以防止此类冲突。请参阅下面的代码以及有关如何执行此操作的注释。
var TelegramBot = require('node-telegram-bot-api'),
bot = new TelegramBot("MY_TOKEN", {
polling: true
});
bot.onText(/^\/createroom/, function (res, match) {
//use res.chat.id for groups and res.user.id for individuals
service.checkIfRoomExist(res.chat.id).then(function (isExist) {
if (isExist === false) {
service.createRoom(res.chat.id).then(function () {
bot.sendMessage(res.chat.id, 'Initializing game!')
// send game content here
});
}
bot.sendMessage(res.chat.id, 'A game has already started in this group!')
})
});
function checkIfRoomExist(id) {
// Your logic here that checks in database if game has been created
}
关于javascript - Telegram - Node 。 JS处理多次更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40827823/