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


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

Node.js Telegraf Bot模塊中使用了Bot.help()方法。該模塊提供了與官方Telegram Bot API交互的各種函數。當我們鍵入保留關鍵字/help時,將執行此方法。當我們有任何與漫遊器函數相關的查詢並返回與您的查詢相關的輸出時,通常使用此方法。

用法:

TelegrafBot.help(callback)

參數:此方法接受上麵提到的和下麵描述的一個參數:

  • 回調函數:此方法僅接受一個包含Telegram API中的Update對象的參數。

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

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



npm install telegraf

獲取 key 的步驟:

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

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

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

  • 現在,隻需從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); 
  
bot.help(ctx => { 
  
    // The ctx object holds the Update 
    // object from Telegram API 
    ctx.reply("Yes what can i help"); 
  
    bot.hears("What is your name", ctx => { 
          
        // Reply with your custom message 
        ctx.reply("Hi This bot Created by Zack_aayush") 
    }) 
}) 
  
// Calling the launch function 
bot.launch()

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

node bot.js

輸出:

相關用法


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