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

表达爱意的程序

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.mzph.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可能都是不一…