Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples: store var LastMessage separertely to prevent race condition between users #239

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/edit-caption.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const TeleBot = require('../');
const bot = new TeleBot('TELEGRAM_BOT_TOKEN');

var lastMessage;
var lastMessage = {};
var photoUrl = 'https://telegram.org/img/tl_card_destruct.gif';

bot.on('/start', msg => {
Expand All @@ -11,19 +11,19 @@ bot.on('/start', msg => {
msg.from.id, photoUrl, {caption: 'This is a default caption.'}
).then(re => {
// Get message id and chat
lastMessage = [msg.from.id, re.result.message_id];
lastMessage[msg.from.id] = [msg.from.id, re.result.message_id];
bot.sendMessage(msg.from.id, 'Now set a new caption using /edit <caption>');
});

});

bot.on('/edit', msg => {

if (!lastMessage) {
if (!lastMessage[msg.from.id]) {
return bot.sendMessage(msg.from.id, 'Type /start and then /edit <caption>');
}

let [chatId, messageId] = lastMessage;
let [chatId, messageId] = lastMessage[msg.from.id];
let caption = msg.text.replace('/edit ', '');

if (caption == '/edit') caption = 'No caption.';
Expand Down
8 changes: 4 additions & 4 deletions examples/edit-markup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const TeleBot = require('../');
const bot = new TeleBot('TELEGRAM_BOT_TOKEN');

var lastMessage;
var lastMessage = {};

bot.on('/start', msg => {

Expand All @@ -11,7 +11,7 @@ bot.on('/start', msg => {
msg.from.id, 'This is a editMessageReplyMarkup example. So, apples or oranges?', {markup}
).then(re => {
// Start updating message
lastMessage = [msg.from.id, re.result.message_id];
lastMessage[msg.from.id] = [msg.from.id, re.result.message_id];
});

});
Expand All @@ -22,10 +22,10 @@ bot.on('callbackQuery', msg => {
// Send confirm
bot.answerCallbackQuery(msg.id);

if (!lastMessage) return bot.sendMessage(msg.from.id, 'Type /start');
if (!lastMessage[msg.from.id]) return bot.sendMessage(msg.from.id, 'Type /start');

const data = msg.data;
const [chatId, messageId] = lastMessage;
const [chatId, messageId] = lastMessage[msg.from.id];
const replyMarkup = updateKeyboard(data);

// Edit message markup
Expand Down