表達愛意的程序_如何像程序員一樣表達愛意??

表達愛意的程序

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/

表達愛意的程序

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390470.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390470.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390470.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

工作中的小問題

1、a標簽的選擇問題 需要修改帶class的a標簽的hover的文字顏色&#xff0c;方式如下 <style>a.egHyperlink:hover{color:red;} </style> <a href"#" class"egHyperlink">smile</a> 復制代碼2、hr分割線 需要一條粉紅色的分割線&am…

More DETAILS! PBR的下一個發展在哪里?

最近幾年圖形學社區對PBR的關注非常高&#xff0c;也許是由于Disney以及一些游戲引擎大廠的助推&#xff0c;也許是因為它可以被輕松集成進實時渲染的游戲引擎當中&#xff0c;也許是因為許多人發現現在只需要調幾個參數就能實現具有非常精細細節的表面著色了。反正現在網絡上隨…

sql server 2008 身份驗證失敗 18456

雙擊打開后加上 ;-m 然后以管理員方式 打開 SQLSERVER 2008 就可以已window身份登錄 不過還沒有完 右鍵 屬性 》安全性 更改為 sql server 和 window身份驗證模式 沒有sql server登陸賬號的話創建一個 然后把-m去掉就可以用帳號登錄了 轉載于:https://www.cnblogs.com/R…

js 兩個方法

//js in_array方法function in_array(all,one) { for(i0;i<all.length;i) { if(all[i] one) return true; } return false; } //js in_array方法/*** 一維數組去重方法** param arr 需要去重數組* returns {Array} 返回已經去重數組*/function unique(arr) {var ret [];va…

敏捷數據科學pdf_如何將敏捷框架應用于數據科學項目

敏捷數據科學pdfIn this article, well discuss how agile principles and values can be applied to the way you approach data science projects.在本文中&#xff0c;我們將討論如何將敏捷性原則和價值觀應用于您處理數據科學項目的方式。 Project management methodologi…

劍指 Offer 56 - II. 數組中數字出現的次數 II

在一個數組 nums 中除一個數字只出現一次之外&#xff0c;其他數字都出現了三次。請找出那個只出現一次的數字。 示例 1&#xff1a; 輸入&#xff1a;nums [3,4,3,3] 輸出&#xff1a;4 示例 2&#xff1a; 輸入&#xff1a;nums [9,1,7,9,7,9,7] 輸出&#xff1a;1 限制…

Java逆向基礎之AspectJ的獲取成員變量的值

注意&#xff1a;由于JVM優化的原因&#xff0c;方法里面的局部變量是不能通過AspectJ攔截并獲取其中的值的&#xff0c;但是成員變量可以在逆向中&#xff0c;我們經常要跟蹤某些類的成員變量的值&#xff0c;這里以獲取ZKM9中的qs類的成員變量g為例進行說明在StackOverFlow上…

鹽噪聲和胡椒噪聲的區別_為什么加一點鹽對您的密碼很有用(但不包括胡椒粉!)

鹽噪聲和胡椒噪聲的區別A brief note - this article is about the theory of how to crack hashed passwords. Understanding how cybercriminals execute attacks is extremely important for understanding how to secure systems against those types of attacks. 簡要說明…

【Luogu1393】動態逆序對(CDQ分治)

【Luogu1393】動態逆序對&#xff08;CDQ分治&#xff09; 題面 題目描述 對于給定的一段正整數序列&#xff0c;我們定義它的逆序對的個數為序列中ai>aj且i < j的有序對(i,j)的個數。你需要計算出一個序列的逆序對組數及其刪去其中的某個數的逆序對組數。 輸入輸出格式 …

iOS底層原理探究-Runloop

Runloop 1. 概述 一般來說&#xff0c;一個線程只能執行一個任務&#xff0c;執行完就會退出&#xff0c;如果我們需要一種機制&#xff0c;讓線程能隨時處理時間但并不退出&#xff0c;那么 RunLoop 就是這樣的一個機制。Runloop是事件接收和分發機制的一個實現。 RunLoop實際…

p2020開發_2020年最佳開發者社區

p2020開發If you want to grow as a developer, I cant over-emphasize the benefits of joining a developer community. There are many advantages, from peer-programming to sharing knowledge, mentorship, sharing support, sharing tools, code reviews, answering que…

leetcode 1838. 最高頻元素的頻數

元素的 頻數 是該元素在一個數組中出現的次數。 給你一個整數數組 nums 和一個整數 k 。在一步操作中&#xff0c;你可以選擇 nums 的一個下標&#xff0c;并將該下標對應元素的值增加 1 。 執行最多 k 次操作后&#xff0c;返回數組中最高頻元素的 最大可能頻數 。 示例 1&…

Deepin系統手動安裝oracle jdk8詳細教程

Deepin系統手動安裝oracle jdk8詳細教程 oracle官網下載jdk壓縮包&#xff0c;使用 sudo tar -zxf jdk***解壓文件&#xff0c;我放在在了home/diy/java/jdk路徑下。 jdk文件路徑&#xff1a;/home/diy/java/jdk/jdk1.8.0_152 JDK環境變量配置 修改配置文件 sudo vi /etc/profi…

Spark 鍵值對RDD操作

https://www.cnblogs.com/yongjian/p/6425772.html 概述 鍵值對RDD是Spark操作中最常用的RDD&#xff0c;它是很多程序的構成要素&#xff0c;因為他們提供了并行操作各個鍵或跨界點重新進行數據分組的操作接口。 創建 Spark中有許多中創建鍵值對RDD的方式&#xff0c;其中包括…

無服務器架構_如何開始使用無服務器架構

無服務器架構Traditionally, when you wanted to build a web app or API, you’d usually have to spend significant time and effort managing servers and ensuring your app scales up to handle large request volumes. Serverless is a cloud computing model which let…

WPF中的動畫——(一)基本概念

原文:WPF中的動畫——&#xff08;一&#xff09;基本概念WPF的一個特點就是支持動畫&#xff0c;我們可以非常容易的實現漂亮大方的界面。首先&#xff0c;我們來復習一下動畫的基本概念。計算機中的動畫一般是定格動畫&#xff0c;也稱之為逐幀動畫&#xff0c;它通過每幀不同…

cloud 異步遠程調用_異步遠程工作的意外好處-以及如何擁抱它們

cloud 異步遠程調用In this article, Ill discuss the positive aspects of being a little out of sync with your team.在本文中&#xff0c;我將討論與您的團隊有點不同步的積極方面。 So you’ve started working from home.因此&#xff0c;您已經開始在家工作。 There …

linux 問題一 apt-get install 被 lock

問題&#xff1a; sudo apt-get install vim E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it? 解決&#xff1a; sudo rm /var/cac…

工信部高級軟件工程師_作為新軟件工程師的信

工信部高級軟件工程師Dear Self, 親愛的自我&#xff0c; You just graduated and you are ready to start your career in the IT field. I cannot spoil anything, but I assure you it will be an interesting ride. 您剛剛畢業&#xff0c;就可以開始在IT領域的職業了。 我…

Python高級網絡編程系列之基礎篇

一、Socket簡介 1、不同電腦上的進程如何通信&#xff1f; 進程間通信的首要問題是如何找到目標進程&#xff0c;也就是操作系統是如何唯一標識一個進程的&#xff01; 在一臺電腦上是只通過進程號PID&#xff0c;但在網絡中是行不通的&#xff0c;因為每臺電腦的IP可能都是不一…