表達愛意的程序
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:00
: 00:00, 01:00, 02:00
: 00:00, 01:00, 02:00
: 00: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 SID
和AUTH 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_SID
和AUTH_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()
函數需要三件事:
- The body/the message 正文/信息
- The FROM number (this is the trial number created above) FROM號碼(這是上面創建的試用號碼)
- 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/
表達愛意的程序