sender是一个用于通过Telegram发送消息的JavaScript库。使用sender,您可以轻松地通过Telegram API发送文本、图片、视频等多种类型的消息。
使用npm安装sender:
npm install yuka-sender --save
在使用sender之前,您需要拥有一个Telegram Bot并获取其API Token。可以通过BotFather创建一个新的Bot并获得其Token。
const Sender = require('yuka-sender');
// 在这里输入您的API Token
const TOKEN = 'YOUR_API_TOKEN';
// 创建一个Sender实例
const sender = new Sender(TOKEN);
使用sender可以轻松地向Telegram用户发送文本消息。
const chatId = 'USER_CHAT_ID';
const text = 'Hello, World!';
sender.sendMessage(chatId, text)
.then(() => console.log('Message sent!'))
.catch((err) => console.error(err));
使用sender可以发送图片消息。
const chatId = 'USER_CHAT_ID';
const photoPath = '/path/to/photo.jpg';
sender.sendPhoto(chatId, photoPath)
.then(() => console.log('Photo sent!'))
.catch((err) => console.error(err));
使用sender可以发送视频消息。
const chatId = 'USER_CHAT_ID';
const videoPath = '/path/to/video.mp4';
sender.sendVideo(chatId, videoPath)
.then(() => console.log('Video sent!'))
.catch((err) => console.error(err));
使用sender可以发送音频消息。
const chatId = 'USER_CHAT_ID';
const audioPath = '/path/to/audio.mp3';
sender.sendAudio(chatId, audioPath)
.then(() => console.log('Audio sent!'))
.catch((err) => console.error(err));
使用sender可以发送文件消息。
const chatId = 'USER_CHAT_ID';
const filePath = '/path/to/file.pdf';
sender.sendFile(chatId, filePath)
.then(() => console.log('File sent!'))
.catch((err) => console.error(err));
使用sender还可以执行其他方法,例如向多个用户发送相同消息、向指定用户发送键盘等。
// 向多个用户发送相同消息
const chatIds = ['USER_CHAT_ID_1', 'USER_CHAT_ID_2'];
const text = 'Hello, World!';
sender.sendMessages(chatIds, text)
.then(() => console.log('Messages sent!'))
.catch((err) => console.error(err));
// 向指定用户发送键盘
const chatId = 'USER_CHAT_ID';
const keyboard = {
inline_keyboard: [
[{text: 'Button 1', callback_data: 'button_1'}],
[{text: 'Button 2', callback_data: 'button_2'}]
]
};
sender.sendMessage(chatId, 'Choose a button:', { reply_markup: keyboard })
.then(() => console.log('Keyboard sent!'))
.catch((err) => console.error(err));
本库是基于telegram-bot-api库构建的,感谢yagop为我们提供了如此优秀的开源库!