javascript - Telegram - Node 。 JS处理多次更新

标签 javascript node.js telegram telegram-bot

我正在 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/

相关文章:

javascript - 添加 Font Awesome 图标

javascript - 如何使用 busboy 防止 node.js Express 中的 POST 垃圾邮件?

javascript - 分割字符串并用复选框显示结果

javascript - 两个循环并且未连接到Mysql

node.js - 我如何找出nodejs支持哪个版本的angular-cli?

javascript - 没有默认按钮的 Telegram 授权

javascript - 在文本框中按回车键时,如何使用 JQuery 关注提交按钮?

javascript - Express的req.params格式是什么?

java - Android Telegram 应用程序 --> java.lang.UnsatisfiedLinkError : No implementation found for void

Telegram bot api - 获取组中的所有消息