Bot.on()方法在Node.js現代Tele graf Bot框架中使用。該框架提供了與官方Telegram Bot API交互的各種函數。當與機器人進行對話期間發生特定事件時,例如,諸如發送文本,發送照片等事件,這些操作由Context函數處理。句法:
TelegrafBot.on(event, Context function)
參數:該方法接受上述和以下描述的兩個參數:
1.Event:與bot對話期間發生特定活動。 2.Context函數:該函數封裝電報更新信息。
返回類型:該函數的返回類型為void。
安裝模塊:使用以下命令安裝模塊:
npm install telegraf
獲取 key 的步驟:
1.首先,通過電報從BOTFATHER獲取GET BOT_TOKEN。隻需在Telegram中搜索BOTFATHER,然後選擇經過驗證的BOTFATHER,如下所示:
2.鍵入/start,然後單擊/newbot,如下所示:
3.現在輸入機器人的名稱,並且該名稱必須唯一。
4.現在隻需從BotFather複製令牌。對於刪除令牌,隻需在BotFather中搜索/delete令牌即可。
項目結構:
檔案名稱:bot.js
javascript
var token = 'Enter the token';
const telegraf =require("telegraf");
var token='Token';
const bot=new telegraf(token); //Creating object of Telegraf
bot.on("text",ctx=>{
//"Event of the text"
ctx.reply("This is the text") //context function reply the message
})
bot.on("photo",ctx=>{
ctx.reply("This is the photo"); //context function reply the message
})
});
使用以下命令運行bot.js文件:
node bot.js
輸出:
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js fs.fsyncSync()用法及代碼示例
- Node.js GM charcoal()用法及代碼示例
- Node.js GM blur()用法及代碼示例
- Node.js GM sharpen()用法及代碼示例
- Node.js GM drawLine()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
注:本文由純淨天空篩選整理自zack_aayush大神的英文原創作品 Node.js Bot.on() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。