node-telegram-bot-api 是一个用于与 Telegram Bot API 交互的 Node.js 库。它提供了丰富的接口,支持各种 Telegram 机器人的功能。TelegramBot 类是核心类,包含了大量的方法来处理与 Telegram 机器人的交互。
以下是 TelegramBot 类中常用的方法及其功能概述:
常用方法
1. 发送消息
-
sendMessage(chatId, text, [options])- 向指定的聊天发送文本消息。
chatId是接收消息的聊天 ID。text是发送的消息内容。options是可选参数,可以包括消息格式、键盘等。
bot.sendMessage(chatId, 'Hello, World!', { parse_mode: 'Markdown' });
2. 编辑消息
-
editMessageText(text, [options])- 编辑已经发送的消息文本。
text是新的消息内容。options必须包含chat_id和message_id或inline_message_id。
bot.editMessageText('New Text', { chat_id: chatId, message_id: messageId }); -
editMessageCaption(caption, [options])- 编辑消息的标题或描述。
caption是新的标题或描述。options必须包含chat_id和message_id或inline_message_id。
bot.editMessageCaption('New Caption', { chat_id: chatId, message_id: messageId }); -
editMessageReplyMarkup(replyMarkup, [options])- 编辑消息的回复标记(如键盘)。
replyMarkup是新的回复标记。options必须包含chat_id和message_id或inline_message_id。
bot.editMessageReplyMarkup({ inline_keyboard: [...] }, { chat_id: chatId, message_id: messageId });
3. 删除消息
-
deleteMessage(chatId, messageId)- 删除指定的消息。
chatId是消息所在的聊天 ID。messageId是要删除的消息 ID。
bot.deleteMessage(chatId, messageId);
4. 发送照片
-
sendPhoto(chatId, photo, [options])- 向指定的聊天发送照片。
photo可以是文件 ID、本地文件路径或 URL。options是可选参数,如标题等。
bot.sendPhoto(chatId, 'photo_url_or_path', { caption: 'This is a photo' });
5. 发送音频、文档、视频
-
sendAudio(chatId, audio, [options])- 发送音频文件。
audio可以是文件 ID、本地文件路径或 URL。options包含音频的附加信息,如标题、持续时间等。
bot.sendAudio(chatId, 'audio_file_path', { title: 'Audio Title' }); -
sendDocument(chatId, document, [options])- 发送文档。
document可以是文件 ID、本地文件路径或 URL。
bot.sendDocument(chatId, 'document_file_path'); -
sendVideo(chatId, video, [options])- 发送视频文件。
video可以是文件 ID、本地文件路径或 URL。
bot.sendVideo(chatId, 'video_file_path');
6. 发送其他媒体
-
sendVoice(chatId, voice, [options])- 发送语音消息。
bot.sendVoice(chatId, 'voice_file_path'); -
sendVideoNote(chatId, videoNote, [options])- 发送视频笔记(小视频)。
bot.sendVideoNote(chatId, 'video_note_file_path'); -
sendAnimation(chatId, animation, [options])- 发送动画(GIF)。
bot.sendAnimation(chatId, 'animation_file_path');
7. 发送位置和联系人
-
sendLocation(chatId, latitude, longitude, [options])- 发送地理位置。
bot.sendLocation(chatId, 48.8588443, 2.2943506); // Eiffel Tower coordinates -
sendContact(chatId, phoneNumber, firstName, [options])- 发送联系人信息。
bot.sendContact(chatId, '+123456789', 'John Doe');
8. 管理聊天和用户
-
getChat(chatId)- 获取聊天的信息。
bot.getChat(chatId).then(chat => console.log(chat)); -
getChatAdministrators(chatId)- 获取聊天管理员列表。
bot.getChatAdministrators(chatId).then(admins => console.log(admins)); -
getChatMembersCount(chatId)- 获取聊天成员数量。
bot.getChatMembersCount(chatId).then(count => console.log(count)); -
getChatMember(chatId, userId)- 获取特定用户在聊天中的状态。
bot.getChatMember(chatId, userId).then(member => console.log(member));
9. 管理键盘
-
sendMessage(chatId, text, { reply_markup })- 使用自定义键盘发送消息。
bot.sendMessage(chatId, 'Choose an option', { reply_markup: { keyboard: [['Option 1', 'Option 2'], ['Option 3']] } }); -
sendMessage(chatId, text, { reply_markup })- 使用 inline 键盘发送消息。
bot.sendMessage(chatId, 'Choose an option', { reply_markup: { inline_keyboard: [ [{ text: 'Option 1', callback_data: '1' }], [{ text: 'Option 2', callback_data: '2' }] ] } });
10. 处理更新和消息
-
on(event, listener)- 监听特定事件。
- 常见事件包括
message,callback_query,inline_query等。
bot.on('message', (msg) => { console.log('Received message:', msg); }); -
processUpdate(update)- 手动处理一个更新(通常用于 Webhook 模式)。
bot.processUpdate(req.body);
其他功能
除了以上常用的方法,node-telegram-bot-api 还支持丰富的功能:
- 发送投票(poll):
sendPoll(chatId, question, options, [extraOptions]) - 发送聊天动作:
sendChatAction(chatId, action)用于表示机器人正在“打字”或“上传照片”等。 - 获取文件信息和下载文件:
getFile(fileId)和downloadFile(fileId, downloadDir) - 管理群组和频道:
kickChatMember,unbanChatMember,restrictChatMember,promoteChatMember等方法。 - 回答内联查询:
answerInlineQuery(inlineQueryId, results, [options]) - 回答回调查询:
answerCallbackQuery(callbackQueryId, [options])
参考资料
你可以在 node-telegram-bot-api 官方文档 上找到更详细的 API 参考和使用示例。文档中提供了所有支持的方法及其详细说明和使用示例。
评论
使用 GitHub 账号登录后即可评论