表达爱意的程序_如何像程序员一样表达爱意❤️

表达爱意的程序

Today is Valentines Day! 😍

今天是情人节! 😍

How nice would it be if you sent a Romantic Message every hour to your loved one? But even better...

如果您每小时向您所爱的人发送一封浪漫的短信,那将有多好? 但是更好...

How awesome would it be to do it automatically using a Node.js script? We are after all... programmers, right? 😏

使用Node.js脚本自动执行该操作有多棒? 我们毕竟是程序员,对吧? 😏

In this short tutorial I'm going to show you how to do it.

在这个简短的教程中,我将向您展示如何做。

P.S. For the lazy ones out there, here is a Video Tutorial:

PS对于那些懒惰的人,这里有一个视频教程:

创建CRON作业 (Create a CRON job)

First, we need to create a CRON job which will run a function every hour.

首先,我们需要创建一个CRON作业,该作业将每小时运行一次函数。

For that, let's install the node-cron package into our NodeJS app:

为此,让我们将node-cron软件包安装到我们的NodeJS应用程序中:

npm install node-cron

npm install node-cron

Next, we're going to schedule a function to run every hour:

接下来,我们将计划一个每小时运行的函数:

const cron = require('node-cron');

cron.schedule('0 * * * *', () => {
	sendMessage();
});

Perfect. We don't have the sendMessage() function yet, but we'll create it later.

完善。 我们还没有sendMessage()函数,但是稍后再创建。

Also, in case you don't know how the CRON string works, here is an awesome website where you can test it out.

另外,如果您不知道CRON字符串的工作原理,那么可以在此网站上进行测试,这很棒

Basically '0 * * * *' means: Run every hour at 0 minutes, so it will run at: 00:00, 01:00, 02:00, etc... You get the point!

基本上'0 * * * *'意思是: Run every hour at 0 minutes ,因此它将在00:00, 01:00, 02:0000:00, 01:00, 02:0000:00, 01:00, 02:0000:00, 01:00, 02:00等运行,您明白了!

连接到Twilio (Connect to Twilio)

We need a Twilio acount, so head over to Twilio.com and create one. You need to verify your email address and also verify the number you want to send the message to. (I had to "steal" my wife's phone in order to verify the number 😅)

我们需要一个Twilio帐户,所以请转到Twilio.com并创建一个。 您需要验证您的电子邮件地址,并还要验证要将邮件发送到的号码。 (我不得不“偷”我妻子的电话以验证电话号码😅)

In there they'll ask you a couple of questions like: "What programming language are you using?" You can pick Node.js and then you'll end up on the /console page.

在这里,他们会问您几个问题,例如:“您使用的是哪种编程语言?” 您可以选择Node.js,然后进入/console页面。

Here you'll get the ACCOUNT SID and AUTH TOKEN. We need these to call the Twilio API so we are going to store them inside a config.js file.

在这里,您将获得ACCOUNT SIDAUTH TOKEN 。 我们需要这些来调用Twilio API,因此我们将它们存储在config.js文件中。

Warning: Do not share the AUTH TOKEN. This is a secret key so we're going to store them inside this "secret" config.js file.

警告:请勿共享AUTH TOKEN 。 这是一个秘密密钥,因此我们将它们存储在此“秘密” config.js文件中。

Great.

大。

The next thing will be to create a Trial Number (you can find the button on the /console page). This number will be used to send the messages FROM.

接下来是创建一个试用编号(您可以在/console页面上找到该按钮)。 该号码将用于发送来自的消息。

Now that we have everything in place, let's go back to our code!

现在我们已经准备好一切,让我们回到我们的代码!

We need to install the Twilio package: npm install twilio and we need to use the data we stored inside the ./config.js file.

我们需要安装Twilio软件包: npm install twilio ,我们需要使用存储在./config.js文件中的数据。

Along with the ACCOUNT_SID and AUTH_TOKEN we can also store the PHONE_NR of our loved one as we are going to use this to tell Twilio where to send the message TO.

除了ACCOUNT_SIDAUTH_TOKEN我们还可以存储我们所爱的人的PHONE_NR ,因为我们将使用它来告诉Twilio将消息发送到哪里。

Let's do that and also let's create the sendMessage() function, which will... send a message 😆.

让我们执行此操作,还创建sendMessage()函数,该函数将...发送一条消息。

const config = require('./config');
const accountSid = config.ACCOUNT_SID;
const authToken = config.AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);

function sendMessage() {
	client.messages
		.create({
			body: 'Your Message here',
			from: '+19166191713',
			to: config.PHONE_NR
		})
		.then(message => {
			console.log(message);
		});
}

You can see that the client.messages.create() function required three things:

您可以看到client.messages.create()函数需要三件事:

  1. The body/the message

    正文/信息
  2. The FROM number (this is the trial number created above)

    FROM号码(这是上面创建的试用号码)
  3. The TO number (this is the number we want to send the message to)

    TO号码(这是我们要将邮件发送到的号码)

取得讯息 (Get the messages)

We need a list of 24 romantic messages, so for that let's create a messages.js file and put all the messages in there inside an array.

我们需要一个包含24条浪漫消息的列表,为此,我们创建一个messages.js文件,并将所有消息放入数组中。

module.exports = [
	`If I could give you one thing in life, I'd give you the ability to see yourself through my eyes, only then would you realize how special you are to me.`,
	`If you were a movie, I'd watch you over and over again.`,
	`In a sea of people, my eyes always search for you.`
];

I only added 3 messages above, but you can fill the array up until you get to 24 messages.

我仅在上面添加了3条消息,但是您可以填充数组直到获得24条消息。

结合一切 (Combine everything)

Now that we have all the 3 components:

现在,我们拥有所有3个组件:

  • the CRON job

    CRON工作
  • the Twilio sendMessage() call

    Twilio sendMessage()调用
  • the messages

    消息

We can combine them into the final code:

我们可以将它们合并为最终代码:

const cron = require('node-cron');

const config = require('./config');
const accountSid = config.ACCOUNT_SID;
const authToken = config.AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);

const messages = require('./messages');

const currentMessage = 0;

function sendMessage() {
	client.messages
		.create({
			body: messages[currentMessage],
			from: '+19166191713',
			to: config.PHONE_NR
		})
		.then(message => {
			currentMessage++;
			console.log(message);
		});
}

cron.schedule('0 * * * *', () => {
	console.log('Message sent!');
	sendMessage();
});

You can see that I added a currentMessage counter which will be incremented every time we send a message, this way we're going to loop over the entire array of messages.

您可以看到我添加了一个currentMessage计数器,该计数器在每次发送消息时都会增加,这样我们就可以遍历整个消息数组。

That's it! 😃

而已! 😃

Now you can run the script and it'll send a romantic message, every hour, to your loved one!

现在,您可以运行该脚本,它将每小时发送一个浪漫的消息给您所爱的人!

Happy Valentine's! 😇

情人节快乐! 😇

Originally posted on www.florin-pop.com

最初发布在www.florin-pop.com

翻译自: https://www.freecodecamp.org/news/send-a-romantic-message-every-hour-to-your-valentine/

表达爱意的程序

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值