如何清除聊天 NODE JS 电报机器人 (node-telegram-bot-api.)
How to clear chat NODE JS telegram bot (node-telegram-bot-api.)
大家好,我是初级 nodejs 开发人员!
我的问题:如何在写文字时清除聊天 == 类
我的代码
const { chat, message_id } = message
const chatId = message.chat.id
const name = message.from.first_name
const text = message.text
// ================== on write "cls" clear chat
else if (text == 'cls') {
bot.deleteMessage(chatId, chat.id)
var msg = message;
}
我认为目前没有任何方法可以使用机器人清除整个聊天但是
您可以使用简单循环删除最后 100 条消息(仅适用于群聊,机器人应具有删除消息的管理员权限)。像这样,
const Telegram = require('node-telegram-bot-api')
const TOKEN = process.env.TOKEN || "<YOUR BOT TOKEN>";
const bot = new Telegram(TOKEN,{polling:true})
bot.onText(/\/start/,(msg)=>{
bot.sendMessage(msg.chat.id,'Hello World!')
})
//I use bot command regex to prevent bot from miss understanding any user messages contain 'cls'
bot.onText(/\/cls/,(msg)=>{for (let i = 0; i < 101; i++) {
bot.deleteMessage(msg.chat.id,msg.message_id-i).catch(er=>{return})
//if there isn't any messages to delete bot simply return
}
}
)
希望这对你有帮助 :D
参考:
https://core.telegram.org/bots/api#deletemessage
Note: I'm a beginner, so if there are any mistakes, please excuse me.
大家好,我是初级 nodejs 开发人员!
我的问题:如何在写文字时清除聊天 == 类
我的代码
const { chat, message_id } = message
const chatId = message.chat.id
const name = message.from.first_name
const text = message.text
// ================== on write "cls" clear chat
else if (text == 'cls') {
bot.deleteMessage(chatId, chat.id)
var msg = message;
}
我认为目前没有任何方法可以使用机器人清除整个聊天但是 您可以使用简单循环删除最后 100 条消息(仅适用于群聊,机器人应具有删除消息的管理员权限)。像这样,
const Telegram = require('node-telegram-bot-api')
const TOKEN = process.env.TOKEN || "<YOUR BOT TOKEN>";
const bot = new Telegram(TOKEN,{polling:true})
bot.onText(/\/start/,(msg)=>{
bot.sendMessage(msg.chat.id,'Hello World!')
})
//I use bot command regex to prevent bot from miss understanding any user messages contain 'cls'
bot.onText(/\/cls/,(msg)=>{for (let i = 0; i < 101; i++) {
bot.deleteMessage(msg.chat.id,msg.message_id-i).catch(er=>{return})
//if there isn't any messages to delete bot simply return
}
}
)
希望这对你有帮助 :D
参考: https://core.telegram.org/bots/api#deletemessage
Note: I'm a beginner, so if there are any mistakes, please excuse me.