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


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

Bot.sendMessage()方法在Node.js Telegram Bot API中使用。該Node.js模塊可與官方Telegram Bot API進行交互。此方法用於發送擴展名為.pdf,.docx,.txt等的文檔。

用法:

TelegramBot.sendDocument(chatId, location)

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

  • chatId:chatId是聊天的唯一標識符,可以是私有,組,超級組或通道,而userId是僅用於用戶或漫遊器的唯一標識符。每個客戶的消息均包含chatId。
  • Location:我們要以String格式發送的文檔的位置。

返回類型:該函數的返回類型為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];  
  
 // Reply to the Bot 
 bot.sendMessage(chatId, "Your Document is")  
    
 // Sending the document 
 bot.sendDocument(chatId, "document.pdf");  
});

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

node bot.js

輸出:

相關用法


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