多伦多到温莎_我想要freeCodeCamp Toronto的Twitter来发布报价,所以我做了一个免费的bot来做到这一点。...
多伦多到温莎
If you read About time, you’ll know that I’m a big believer in spending time now on building things that save time in the future. To this end, I built a simple Twitter bot in Go that would occasionally post links to my articles and keep my account interesting even when I’m too busy to use it. The tweets help drive traffic to my sites, and I don’t have to lift a finger.
如果您阅读《 关于时间》 ,您会知道我非常相信现在花时间来构建可以节省时间的东西。 为此,我在Go中构建了一个简单的Twitter机器人,该机器人有时会发布指向我文章的链接,即使在我忙于使用它的情况下,也会使我的帐户变得有趣。 这些推文有助于吸引流量到我的网站,而我不必费力。
I ran the bot on an Amazon EC2 instance for about a month. My AWS usage has historically been pretty inexpensive (less than the price of a coffee in most of North America), so I was surprised when the little instance I was using racked up a bill 90% bigger than the month before. I don’t think AWS is expensive, to be clear, but still… I’m cheap. I want my Twitter bot, and I want it for less.
我在Amazon EC2实例上运行该机器人大约一个月。 从历史上看,我对AWS的使用非常便宜(低于北美大部分地区的咖啡价格),所以当我使用的小实例的账单比前一个月大90%时,我感到惊讶。 显然,我认为AWS并不昂贵,但仍然……我很便宜。 我想要我的Twitter机器人,而且想要更少的钱。
I’d been meaning to explore AWS Lamda, and figured this was a good opportunity. Unlike an EC2 instance that is constantly running (and charging you for it), Lambda charges you per request and according to the duration of the time your function takes to run. There’s a free tier, too, and the first 1 million requests, plus a certain amount of compute time, are free.
我一直想探索AWS Lamda,并认为这是一个很好的机会。 与不断运行(并向您收费)的EC2实例不同,Lambda会根据您的请求并根据功能运行的持续时间向您收费。 也有一个免费层,前一百万个请求以及一定数量的计算时间是免费的。
Roughly translated to running a Twitter bot that posts for you, say, twice a day, your monthly cost for using Lambda would total… carry the one… nothing. I’ve been running my Lambda function for a couple weeks now, completely free.
粗略地解释为运行一个Twitter机器人,该机器人每天为您发布两次,您每月使用Lambda的费用总计……携带一个……什么也没有。 我已经完全免费地运行Lambda函数两周了。
When it recently came time for me to take the reigns of the @freeCodeCampTO Twitter, I decided to employ a similar strategy. I also used this opportunity to document the process for you, dear reader.
当我最近要接受@freeCodeCampTO Twitter的统治时,我决定采用类似的策略。 亲爱的读者,我也借此机会为您记录了过程。
So if you’re currently using a full-time running instance for a task that could be served by a cron job, this is the article for you. I’ll cover how to write your function for Lambda, and how to get it set up to run automatically. And, as a sweet little bonus, I’ll include a handy bash script that updates your function from the command line whenever you need to make a change. Let’s do it!
因此,如果您当前正在使用一个全时运行的实例来执行可由cron作业执行的任务,那么本文适合您。 我将介绍如何为Lambda编写函数,以及如何将其设置为自动运行。 而且,作为一点好处,我将包含一个方便的bash脚本,该脚本可在需要更改时从命令行更新您的功能。 我们开始做吧!
Lambda适合您吗? (Is Lambda right for you?)
When I wrote the code for my Twitter bot in Go, I intended to have it run on an AWS instance and I borrowed heavily from Francesc’s awesome Just for Func episode. Some time later, I modified it to randomly choose an article from my RSS feeds and to tweet the link, twice a day. I wanted to do something similar for the @freeCodeCampTO bot, and have it tweet an inspiring quote about programming every morning.
当我在Go中为Twitter机器人编写代码时,我打算让它在AWS实例上运行,并从Francesc出色的Just for Func剧集中大量借用了代码。 一段时间后,我对其进行了修改,以从RSS提要中随机选择一篇文章,并每天两次推文链接。 我想为@freeCodeCampTO机器人做类似的事情,并每天早上在推特上发一条激励人心的报价。
This is a good use case for Lambda because:
这对于Lambda是一个很好的用例,因为:
- The program should execute once该程序应执行一次
- It runs on a regular schedule, using time as a trigger它以时间为触发,定期运行
- It doesn’t need to run constantly不需要持续运行
The important thing to keep in mind is that Lambda runs a function once in response to an event that you define. The most widely applicable trigger is a simple cron expression, but there are many other trigger events you can hook up. You can get an overview here.
要记住的重要一点是,Lambda会针对您定义的事件运行一次函数。 适用范围最广的触发器是一个简单的cron表达式,但是您还可以关联许多其他触发器事件。 您可以在此处获得概述。
编写Lambda函数 (Write a Lambda function)
I found this really straightforward to do in Go. First, grab the aws-lambda-go library:
我发现在Go中确实很简单。 首先,获取aws-lambda-go库:
go get github.com/aws/aws-lambda-go/lambda
Then make this your func main()
:
然后将其func main()
:
func main() { lambda.Start(tweetFeed)
}
where tweetFeed
is the name of the function that makes everything happen. While I won’t go into writing the whole Twitter bot here, you can view my code on GitHub.
其中tweetFeed
是使所有事情发生的函数的名称。 虽然我不会在这里编写整个Twitter机器人,但是您可以在GitHub上查看我的代码 。
设置AWS Lambda (Setting up AWS Lambda)
I’m assuming you already have an AWS account. If not, first things first here: https://aws.amazon.com/free
我假设您已经有一个AWS账户。 如果不是这样,那么首先要做的是: https : //aws.amazon.com/free
1.创建您的功能 (1. Create your function)
Find AWS Lambda in the list of services, then look for this shiny button:
在服务列表中找到AWS Lambda,然后寻找这个闪亮的按钮:
We’re going to author a function from scratch. Name your function, then under Runtime choose “Go 1.x”.
我们将从头开始编写一个函数。 命名您的函数,然后在运行时下选择“ Go 1.x”。
Under Role name write any name you like. It’s a required field, but irrelevant for this use case.
在“ 角色名称”下输入您喜欢的任何名称。 这是必填字段,但与该用例无关。
Click Create function.
单击创建功能。
2.配置您的功能 (2. Configure your function)
You’ll see a screen for configuring your new function. Under Handler enter the name of your Go program.
您会看到一个用于配置新功能的屏幕。 在处理程序下,输入Go程序的名称。
If you scroll down, you’ll see a spot to enter environment variables. This is a great place to enter the Twitter API tokens and secrets, using the variable names that your program expects. The AWS Lambda function will create the environment for you using the variables you provide here.
如果向下滚动,则会看到一个输入环境变量的位置。 这是使用程序期望的变量名输入Twitter API令牌和机密的好地方。 AWS Lambda函数将使用您在此处提供的变量为您创建环境。
No further settings are necessary for this use case. Click Save at the top of the page.
此用例无需其他设置。 点击页面顶部的保存 。
3.上传您的代码 (3. Upload your code)
You can upload your function code as a zip file on the configuration screen. Since we’re using Go, you’ll want to go build
first, then zip the resulting executable before uploading that to Lambda.
您可以在配置屏幕上将功能代码作为zip文件上传。 由于我们正在使用Go,因此您需要go build
,然后压缩生成的可执行文件,然后再将其上传到Lambda。
…Of course I’m not going to do that manually every time I want to tweak my function. That’s what awscli
and this bash script are for!
…当然,我每次想要调整功能时都不会手动执行此操作。 这就是awscli
和此bash脚本的作用所在!
update.sh
update.sh
go build && \
zip fcc-tweet.zip fcc-tweet && \
rm fcc-tweet && \
aws lambda update-function-code --function-name fcc-tweet --zip-file fileb://fcc-tweet.zip && \
rm fcc-tweet.zip
Now whenever I make a tweak, I just run bash update.sh
.
现在,只要进行调整,就可以运行bash update.sh
。
If you’re not already using AWS Command Line Interface, do pip install awscli
and thank me later. Find instructions for getting set up and configured in a few minutes here under Quick Configuration.
如果您尚未使用AWS Command Line Interface ,请执行pip install awscli
并稍后感谢我。 在此处的 “ 快速配置”下找到几分钟后即可进行设置和配置的说明。
4.测试你的功能 (4. Test your function)
Wanna see it go? Of course you do! Click “Configure test events” in the dropdown at the top.
想看吗? 当然可以! 点击顶部下拉菜单中的“配置测试事件”。
Since you’ll use a time-based trigger for this function, you don’t need to enter any code to define test events in the popup window. Simply write any name under Event name and empty the JSON in the field below. Then click Create.
由于您将为此功能使用基于时间的触发器,因此无需在弹出窗口中输入任何代码来定义测试事件。 只需在“ 事件名称”下写下任何名称,然后在下面的字段中清空JSON。 然后点击创建 。
Click Test at the top of the page, and if everything is working correctly you should see…
点击页面顶部的测试 ,如果一切正常,您应该会看到…
5.设置CloudWatch Events (5. Set up CloudWatch Events)
To run our function as we would a cron job — as a regularly scheduled time-based event — we’ll use CloudWatch. Click CloudWatch Events in the Designer sidebar.
为了像执行cron工作一样运行我们的功能(作为定期安排的基于时间的事件),我们将使用CloudWatch。 单击Designer侧栏中的CloudWatch Events 。
Under Configure triggers, you’ll create a new rule. Choose a descriptive name for your rule without spaces or punctuation, and ensure Schedule expression is selected. Then input the time you want your program to run as a rate expression, or cron expression.
在配置触发器下 ,您将创建一个新规则。 为您的规则选择一个不带空格或标点符号的描述性名称,并确保选择了Schedule表达式 。 然后输入您希望程序以rate表达式或cron表达式运行的时间。
A cron expression looks like this: cron(0 12 * * ? *)
cron表达式如下所示: cron(0 12 * * ? *)
The items in the brackets represent, in order: minutes, hours, day of month, month, day of week, and year. In English, it says: Run at noon (UTC) every day.
括号中的项目按顺序表示:分钟,小时,一个月中的某天,一个月,一周中的某天和年份。 用英语说:每天中午(UTC)运行。
For more on how to write your cron expressions, read this.
有关如何编写cron表达式的更多信息,请阅读此内容。
To find out what the current time in UTC is, click here.
要了解UTC当前时间,请单击此处。
If you want your program to run twice a day, say once at 10am and again at 3pm, you’ll need to set two separate CloudWatch Events triggers and cron expression rules.
如果您希望程序每天运行两次,比如说上午10点一次,下午3点一次,那么您需要设置两个单独的CloudWatch Events触发器和cron表达式规则。
Click Add.
点击添加 。
看着它走 (Watch it go)
That’s all you need to get your Lambda function up and running! Now you can sit back, relax, and do more important things than share your RSS links on Twitter.
这就是启动和运行Lambda功能所需的全部! 现在,您可以高枕无忧,做些比在Twitter上共享RSS链接更重要的事情。
翻译自: https://www.freecodecamp.org/news/running-a-free-twitter-bot-on-aws-lambda-66160eb4de4/
多伦多到温莎
相关文章:

Linux常用命令汇总(持续更新中)
命令说明注意点cat access.log | wc -l统计行数awk命令可以做到同样的想过:cat access.log | awk END {print NR}grep vnc /var/log/messages查看系统报错日志等同于:sudo dmesg -T | grep "(java)"netstat -lnt | grep 590*查看端口状态 nets…

IOS问题汇总:2012-12-18 UIAlertView+UIActionSheet
UIAlertView/UIActionSheet UIAlertView * alertView [[UIAlertView alloc] initWithTitle:“添加场景模式” message:“请输入场景名称” delegate:self cancelButtonTitle:“取消” otherButtonTitles:“确定”, nil];alertView.alertViewStyle UIAlertViewStylePlainTextI…

PHP入门 1 phpstudy安装与配置站点
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 1, 一键安装 phpstudy ; 点击跳转下载; 2.配置站点,点击MySQL 其它选项菜单的站点域名管理;再点击新增 2,点击其他选项菜单点击打开…

singleton设计模式_让我们研究一下Singleton设计模式的优缺点
singleton设计模式by Navdeep Singh通过Navdeep Singh 让我们研究一下Singleton设计模式的优缺点 (Let’s examine the pros and cons of the Singleton design pattern) Design patterns are conceptual tools for solving complex software problems. These patterns are si…

【转】MFC消息映射详解(整理转载)
消息:主要指由用户操作而向应用程序发出的信息,也包括操作系统内部产生的消息。例如,单击鼠标左按钮,windows将产WM_LBUTTONDOWN消息,而释放鼠标左按钮将产生WM_LBUTTONUP消息,按下键盘上的字母键ÿ…

php 2 往数据库添加数据
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 前端代码: function submit_result() { $.post("Controllers/ajaxController.php",{"name": $("#name").val(),"mobile": $("#mo…

设计模式:单例
传统的实现方法:两私一公,涉及线程安全问题(即使有多重检查锁也可以通过反射破坏单例)public class Singleton {private volatile static Singleton instance null;private Singleton () {}public static Singleton getSingleton…

100天59万行代码_如何抽出100天的代码时间
100天59万行代码Life moves pretty fast. If you don’t stop and look around once in a while, you could miss it. — Ferris Bueller生活发展很快。 如果您不停地走动,不时环顾四周,您可能会错过它。 —摩天轮 My time at freeCodeCamp was a fun an…

Mac 安装SecureCRT
scrt-8.0.2-1118.osx_x64.dmg https://pan.baidu.com/s/1miS5XVy 1.下载破解文件 SecureCRT https://pan.baidu.com/s/1eRW5IfS 2. 打开终端执行 chmod x ~/Downloads/SecureCRT 替换破解文件SecureCRT到/Applications/SecureCRT.app/Contents/MacOS/ 3. 打开SecureCRT…

PHP 3 HTML POST带参数请求 后端返回json格式的数据给前端
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 前端代码 <!DOCTYPE html> <html lang"zh"><head><meta charset"UTF-8"><title>Title</title><script src"https://ajax.aspnetcdn.c…

产品经理入门_所以您想成为产品经理? 这就是我的入门方式。
产品经理入门by Melanie Lei由Melanie Lei 所以您想成为产品经理? 这就是我的入门方式。 (So you want to be a product manager? This is how I got started.) One of the most common questions I get asked is, “How do you get a Product Manager job if you…

又拍云SSL证书全新上线,提供一站式HTTPS安全解决方案
互联网快速发展,云服务早已融入每一个人的日常生活,而互联网安全与互联网的发展息息相关,这其中涉及到信息的保密性、完整性、可用性、真实性和可控性。又拍云上线了与多家国际顶级 CA 机构合作的数款OV & EV SSL证书,提供一站…

HDU 1155 Bungee Jumping
题意:英语水平太次…………读了好久好久好久才读懂OTZ James Bond要逃跑,跑到一个桥边上,要跳到地面,桥边有个有弹性的绳子长度为l,如果他跳下去能到达地面,但速度超过10就会摔死,否则能成功降落…

php 4 创建公共的链接数据库php文件并在其它文件引用它
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 创建公共文件 <? //1. 声明字符编码 header("Content-Type:text/html;charsetutf8"); //2. 连接数据库 $linkmysql_connect("localhost","root","root");/…

异步回调地狱_如何逃避异步/等待地狱
异步回调地狱async/await freed us from callback hell, but people have started abusing it — leading to the birth of async/await hell.async / await使我们从回调地狱中解脱出来,但是人们已经开始滥用它-导致async / await地狱的诞生。 In this article, I …

2015年编程之美(资格赛) ---2月29日
时间限制:2000ms单点时限:1000ms内存限制:256MB描述 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期)。 只有闰年有2月29日,满足以下一个条件的年份为闰年: 1. 年份能被4整除但不能被100整除 2. 年份能…

2016多校赛2 A 数学推公式 E 极角排序,组合数(待补) L dp+bitset优化
2016 Multi-University Training Contest 2 A - Acperience 题意:给出w[],求S((w[i]-aB[i])^2)的最小值(B[i]为1或-1)。 tags:一看到就搞了个三分上去,估计是精度问题,一直挖,23333。。 就是把这个公式推下…

php No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方法
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 在PHP请求头加上 header("Access-Control-Allow-Origin: *");

二进制搜索算法_使用安全摄像机镜头解释二进制搜索算法
二进制搜索算法by Julia GeistJulia盖斯特(Julia Geist) 使用安全摄像机镜头解释二进制搜索算法 (Binary Search Algorithms explained using security camera footage) Binary search, also known as half-interval search or logarithmic search, is a search algorithm tha…

Codeforces 460E Roland and Rose(暴力)
题目链接:Codeforces 460E Roland and Rose 题目大意:在以原点为圆心,半径为R的局域内选择N个整数点,使得N个点中两两距离的平方和最大。 解题思路:R最大为30。那么事实上距离圆心距离最大的整数点只是12个最多&#x…

HTML POST提交参数给PHP并返回json,上传execl文件
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文: 需求:AJAX post带参数请求接口,PHP接收后存入数据库;然后返回json数据循环渲染到HTML <!DOCTYPE html> <html lang"zh">&l…

在Ubuntu 12.04 桌面上设置启动器(快捷方式)
在Ubuntu 12.04 桌面上设置启动器(快捷方式)过程讲解: 如下图所示,Eclipse 和 SQLDeveloper 都可以直接双击打开,这些应用程序的启动器都在 /usr/share/applications文件夹下面,进入后将其复制到桌面即可。…

rxswift中hud_如何在RxSwift中运行测试
rxswift中hudby Navdeep Singh通过Navdeep Singh 如何在RxSwift中运行测试 (How to run tests in RxSwift) RxTest and RxBlocking are part of the RxSwift repository. They are made available via separate pods, however, and so require separate imports.RxTest和RxBlo…

安装完DevExpress14.2.5,如何破解它呢?
DevExpress是一个界面控件套件,提供了一系列的界面控件套件的DotNet界面控件。DevExpress开发的控件有很强的实力,不仅功能丰富,应用简单,而且界面华丽,更可方便订制,方便开发人员开发。 下面介绍DevExpres…

Extjs Ext.TreePanel
TreePanel 简单实例。 <link rel"stylesheet" href"Js/ext-4.2/resources/css/ext-all-neptune.css"/><script src"Js/jQuery/jquery-1.8.2.min.js" type"text/javascript"></script><script src"Js/ext-…

php模糊搜索功能
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文: 前端 前端post请求并发送str参数>搜索的内容; PHP <?phpheader("Content-Type:text/html;charsetutf8"); header("Access-Control-Allow-Origin…

react 快速上手开发_React中测试驱动开发的快速指南
react 快速上手开发by Michał Baranowski通过MichałBaranowski React中测试驱动开发的快速指南 (A quick guide to test-driven development in React) Following the principles of Test-Driven Development (TDD) when writing a front-end React app might seem more dif…

iOS 相册和网络图片的存取
iOS 相册和网络图片的存取 保存 UIImage 到相册 UIKit UIKit 中一个古老的方法,Objective-C 的形式 void UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo); 保存完成后,会调用 comple…

微信小程序实时聊天之WebSocket
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文: 1.所有监听事件先在onload监听。 // pages/index/to_news/to_news.js var app getApp(); var socketOpen false; var SocketTask false; var url ws://192.168.0.120:7011; Page…

webform repeater
repeater:由模板构成,解析后模板就不存在了 需要指定数据源进行数据绑定 List<Fruit> list new FruitDA().Select(); // 数据查询 (随便查寻的) Repeater1.DataSource list; // 赋值 Repeater1…