bot.on('message', async (msg) => {
const text = msg.text.toLowerCase()
const chatId = msg.chat.id
const message = 'TEXT'
if (text.match('123')) {
setTimeout(() => {
bot.sendMessage(chatId, message)
setTimeout(() => {
bot.editMessageText(`${message} (edit)`, {
chat_id = chatId,
})
}, 500)
//
}, 500)
}
})
Бот на node-telegram-bot-api.
Нужно изменить сообщение, отправленное телеграм ботом. Как это реализовать?
bot.on('message', async (msg) => {
const text = msg.text.toLowerCase()
const chatId = msg.chat.id
const message = 'TEXT'
if (text.match('123')) {
setTimeout(() => {
bot.sendMessage(chatId, message)
.then(msgData=>{
let count = 0;
const timerId = setInterval(() => {
count+=Math.floor(Math.random()*10);
if(count>100){
count=100;
clearInterval(timerId);
}
bot.editMessageText(`${message} (edit) ${count}%`,
{
chat_id: msgData.chat.id,
message_id: msgData.message_id
});
}, 500)
})
}, 500)
}
})
bot.on('callback_query', async (query) => {
const chatId = query.message.chat.id
const data = query.data
chats = {}
const cuefaArr = ['stone', 'scissors', 'paper', 'lizard', 'spock']
if (data === 'startCuefa') {
await bot.sendMessage(chatId, 'Что выбираете?', {
reply_markup: {
inline_keyboard: [keyboard.gameCuefa],
},
})
} else {
chats[chatId] = helper.random(cuefaArr)
await bot.sendMessage(chatId, chats[chatId])
if (data === chats[chatId]) {
return bot.sendMessage(chatId, 'Ничья.')
} else if (helper.compare(query, chatId, 'lizard', 'scissors', 'stone') || helper.compare(query, chatId, 'paper', 'lizard', 'scissors') || helper.compare(query, chatId, 'spock', 'stone', 'paper') || helper.compare(query, chatId, 'spock', 'paper', 'lizard') || helper.compare(query, chatId, 'stone', 'scissors', 'spock')) {
return bot.sendMessage(chatId, 'Вы проиграли!')
}
}
})
const cuefaHash = {
'stone': ['scissors', 'lizard'],
'scissors': ['paper', 'lizard'],
'paper': ['stone', 'spock'],
'lizard': ['paper', 'spock'],
'spock': ['stone', 'scissors'],
};
const helper = {
random(hash) {
const array = Object.keys(hash);
return array[Math.floor(Math.random() * array.length)];
},
compare(item1, item2) {
if(cuefaHash[item1].includes(item2)) return 'Я ПОБЕДИЛ!!!';
if(cuefaHash[item2].includes(item1)) return 'ВЫ ПОБЕДИЛИ !!!';
return 'НИЧЬЯ !!!';
}
};
bot.onText(/(.+)/, function (msg, match) {
console.log('===========================\n', msg,'\n----------------------------\n');
const opts = {
reply_markup: {
inline_keyboard: [
[{text: 'startCuefa', callback_data: 'startCuefa'}],
]
}
};
bot.sendMessage(msg.from.id, 'Original Text', opts);
});
bot.on('callback_query', async (query) => {
const data = query.data
if (data === 'startCuefa') {
await bot.editMessageText(
'Что выбираете?',
{
chat_id: query.message.chat.id,
message_id: query.message.message_id
}
);
await bot.editMessageReplyMarkup(
{
inline_keyboard: [
Object.keys(cuefaHash).map(item=>({text: item, callback_data: item}))
],
},
{
chat_id: query.message.chat.id,
message_id: query.message.message_id
}
);
} else {
const botSelect = helper.random(cuefaHash);
const text = helper.compare(botSelect, data)
await bot.editMessageText(
`Я выбрал ${botSelect}, вы выбрали ${data}, ${text}\n сыграем еще раз?`,
{
chat_id: query.message.chat.id,
message_id: query.message.message_id
}
);
await bot.editMessageReplyMarkup(
{
inline_keyboard: [
[{text: 'startCuefa', callback_data: 'startCuefa'}],
]
},
{
chat_id: query.message.chat.id,
message_id: query.message.message_id
}
);
}
});