大家好,我是初級 nodejs 開發人員!
我的問題:如何清除寫文字 == cls 上的聊天
我的代碼
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;
}
uj5u.com熱心網友回復:
我認為目前沒有任何方法可以使用 bot 清除整個聊天,但您可以使用簡單回圈洗掉最后 100 條訊息(僅適用于群聊,bot 應具有洗掉訊息的管理員權限)。像這樣,
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
注:本人初學者,如有錯誤請見諒。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/350391.html
