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


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

Bot.sendVoice()方法在Node.js Telegram Bot API中使用。該Node.js模塊可與官方Telegram Bot API進行交互。當用戶與電報BOT交互時,此方法用於答複語音消息。

用法:

TelegramBot.sendVoice(chatId, Location)

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

  • chatId:chatId是聊天的唯一標識符,可以是私有,組,超級組或通道,而userId是僅用於用戶或漫遊器的唯一標識符。每個客戶的消息均包含chatId。
  • Location:第二個參數是語音留言的位置。

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

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



npm i telegram-bot-api

獲取 key 的步驟:

  1. 首先以電報方式從BOTFATHER獲取GET BOT_TOKEN。隻需在Telegram中搜索BOTFATHER,然後選擇經過驗證的BOTFATHER,如下所示:
  2. 鍵入/start,然後單擊/newbot,如下所示:
  3. 現在,鍵入機器人的名稱,並且該名稱必須唯一。
  4. 現在,隻需從BotFather複製令牌。對於刪除令牌,隻需在BotFather中搜索/delete令牌即可。

項目結構:

檔案名稱:bot.js

var token = 'Enter the token'; 
  
const TelegramBot = require('node-telegram-bot-api'); 
  
const bot = new TelegramBot(token, {polling:true}); 
  
// Matches "/echo [whatever]" 
bot.onText(/\/echo(.+)/, (msg, match) => { 
  
 // The 'msg' is the received Message from Telegram 
 // and 'match' is the result of executing the regexp  
 // above on the text content of the message 
  
 const chatId = msg.chat.id; 
  
 // The captured "whatever" 
 const resp = match[1];  
  
 // Send back the matched "whatever" to the chat 
  bot.sendMessage(chatId, "Your Voice message is") 
  
  bot.sendVoice(chatId, "new.mp3"); //Voice Message 
});

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

node bot.js

輸出:

相關用法


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