當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js Bot.start()用法及代碼示例

Bot.start()方法在Node.js Telegraf Bot模塊中使用。該模塊提供了與官方Telegram Bot API交互的各種函數。當新用戶首次啟動bot或鍵入保留的模塊關鍵字/start時,將執行此方法。

用法:

TelegrafBot.start(callback function(Context function))

參數:該方法接受如上所述和以下描述的一個參數:

  • Callback function:它僅接受一個包含來自Telegram API的Update對象的參數。

返回類型:該函數的返回類型為void。

安裝模塊:使用以下命令安裝此模塊:



npm install telegraf

獲取 key 的步驟:

1.首先,從電報中的BOTFATHER獲取GET BOT_TOKEN。隻需在Telegram中搜索BOTFATHER,然後選擇經過驗證的BOTFATHER,如下所示:

2.鍵入/start,然後單擊/newbot,如下所示:

3.現在,鍵入機器人的名稱,並且該名稱必須唯一。

4.現在,隻需從BotFather複製令牌。對於刪除令牌,隻需在BotFather中搜索/delete令牌即可。



項目結構:

檔案名稱:bot.js

Javascript

// Requiring module  
const telegraf = require("telegraf");  
  
// Set your token  
var token = 'YOUR_TOKEN';  
  
// Creating a new object of Telegraf  
const bot = new telegraf(token);  
  
// The ctx object holds the update  
// object from Telegram API 
bot.start( ctx => { 
  
  // Sending the message 
  ctx.reply("Thanking you for chossing new bot");  
}); 
  
// Calling the launch function  
bot.launch()

使用以下命令運行bot.js文件:

node bot.js

輸出:

相關用法


注:本文由純淨天空篩選整理自zack_aayush大神的英文原創作品 Node.js Bot.start() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。