使用Node.js和Telegraf构建你的第一个Telegram机器人
什么是Telegram机器人?机器人是在Telegram应用程序中运行的第三方应用程序账户。用户可以通过向机器人发送消息、命令和内联请求与之互动。你可以使用Telegram Bot API的HTTPS请求来控制你的机器人。
Telegram机器人使Telegram频道和群组中的任务自动化变得容易。你甚至可以建立自己的Telegram机器人,为你策划新闻或监测加密货币趋势。
在本教程中,你将学习如何使用Node.js运行环境和Telegraf库从头开始构建telegram机器人。
这将作为Telegram机器人环境的介绍,使你能够建立天气机器人、供应商机器人、电子商务机器人,甚至是字典机器人。本教程将尽可能地对初学者友好。
前提条件
在你开始本指南之前,你需要以下条件。
- 安装Node.js。
- 安装了npm。
- 安装了Visual Studio Code。
- 使用CLI的基本知识。
- 一个Telegram账户。
设置你的机器人
首先,去Telegram搜索栏搜索@botfather(这是一个特殊的Telegram机器人,控制所有其他的机器人)。我们要用它来为我们的新机器人获得一个令牌。
有几个与@botfather相关的命令,我们将以/newbot开始,然后为机器人提供所需的名称。该名称必须总是以 "bot "结尾。现在我们有了令牌,就可以写代码了。
编码
让我们首先为我们的新项目创建一个文件夹,即:myfirstbot。打开命令行并导航到该文件夹。通过运行以下命令来初始化项目,安装telegraf。
npm init
# install the telegraf library
npm install telegraf
创建一个javascript文件,app.js
const Telegraf = require('telegraf');
const bot = new Telegraf('insert_bot_token_here');
让我们写一个简单的脚本,每次启动机器人时都会欢迎我们。
//method for invoking start command
bot.command('start', ctx => {
console.log(ctx.from)
bot.telegram.sendMessage(ctx.chat.id, 'hello there! Welcome to my new telegram bot.', {
})
})
为了增加更多的功能,让我们编写代码,当你点击内嵌的键盘按钮时,向你显示动物的图像。创建一个res文件夹并将图像添加到其中。
//method that displays the inline keyboard buttons
bot.hears('animals', ctx => {
console.log(ctx.from)
let animalMessage = `great, here are pictures of animals you would love`;
ctx.deleteMessage();
bot.telegram.sendMessage(ctx.chat.id, animalMessage, {
reply_markup: {
inline_keyboard: [
[{
text: "dog",
callback_data: 'dog'
},
{
text: "cat",
callback_data: 'cat'
}
],
]
}
})
})
//method that returns image of a dog
bot.action('dog', ctx => {
bot.telegram.sendPhoto(ctx.chat.id, {
source: "res/dog.jpeg"
})
})
//method that returns image of a cat
bot.action('cat', ctx => {
bot.telegram.sendPhoto(ctx.chat.id, {
source: "res/cat.jpeg"
})
})
我们已经看到了如何使用内联键盘,现在让我们尝试使用一次性回复键盘功能,与机器人分享我们的位置或电话号码。
注意:分享位置功能只适用于Telegram移动应用程序。
//method for requesting user's phone number
bot.hears('phone', (ctx, next) => {
console.log(ctx.from)
bot.telegram.sendMessage(ctx.chat.id, 'Can we get access to your phone number?', requestPhoneKeyboard);
})
//method for requesting user's location
bot.hears("location", (ctx) => {
console.log(ctx.from)
bot.telegram.sendMessage(ctx.chat.id, 'Can we access your location?', requestLocationKeyboard);
})
//constructor for providing phone number to the bot
const requestPhoneKeyboard = {
"reply_markup": {
"one_time_keyboard": true,
"keyboard": [
[{
text: "My phone number",
request_contact: true,
one_time_keyboard: true
}],
["Cancel"]
]
}
};
//constructor for proving location to the bot
const requestLocationKeyboard = {
"reply_markup": {
"one_time_keyboard": true,
"keyboard": [
[{
text: "My location",
request_location: true,
one_time_keyboard: true
}],
["Cancel"]
]
}
}
最后,在结尾处写下以下代码来启动机器人。
//method to start get the script to pulling updates for telegram
bot.launch();
使用机器人
我们已经成功编写了三个代码块,一个是在你启动机器人时欢迎你。
一个是当你点击在线键盘按钮时向你发送图片,最后一个是当用户点击回复键盘按钮时允许他们分享电话号码或位置。
在终端中运行机器人,然后到telegram的搜索栏中搜索你的机器人的名字。
node app.js
下面是telegram的结果。
启动机器人

要求提供狗的照片

要求提供猫的照片

要求提供电话号码

要求提供位置

摘要
在这篇文章中,我们已经了解了如何获得你的Telegram机器人令牌,设置你的环境和编写一个简单的机器人,使用内联键盘功能显示动物的图片,并使用回复键盘功能分享你的位置和电话号码。