一、缘起
最近工作很忙,没时间陪女朋友,再加上她工作比较累,比较忙需要有人每天鼓励她,所以我打算给她每天来点毒鸡汤,然后做个在一起多少天的提示,在查询一下天气,在整点舔狗 暖男必备的骚话,然后每天定时推送。满满的bot。上图的机器人好家伙成本有点大。咱们来整个白嫖 邮件版本的。
二、准备
先从网上整了个天气查询接口:http://wthrcdn.etouch.cn/weather_mini?city=
下面再整个彩虹屁接口:
https://chp.shadiao.app/api.php (感谢 shadiao.app | 2333.jpg)
再整个牛逼的东西叫天行数据注册个账号,免费的接口,每天白嫖100次
这里举两个例子
1.彩虹屁 http://api.tianapi.com/txapi/caihongpi/index?key=APIKEY
2.舔狗日记 http://api.tianapi.com/txapi/tiangou/index?key=APIKEY
3.笑话大全 http://api.tianapi.com/txapi/joke/index?key=APIKEY&num=10
4. ....
三、代码整起
"use strict";
const nodemailer = require("nodemailer");
const http = require("axios").default;
const city = "xx"; //女票所在的城市
const key = "xxx"; //天行数据key
const email_password = "xxx"; //邮箱SMTP密码
const email_user = "xxxxx@qq.com"; //你的邮箱
const addressee = "xxxxx@qq.com"; //你女票的邮箱
const start_time ="2018-08-08"; //在一起的开始时间
var time = Date.now();
var now = new Date(time);
var email_details = "";
async function goodMorning() {
let { data } = await http.get("http://api.tianapi.com/txapi/zaoan/index", {params: { key: key }});
var new_date = new Date();
var old_date = new Date(start_time);
var difftime = (new_date - old_date) / 1000;
var days = parseInt(difftime / 86400);
var morning = data.newslist[0].content;
let weather = await getWeather();
let rainbow_1 = await getRainbow_1();
let rainbow_2 = await getRainbow_2();
let goodMorning = "今天是" + now.getFullYear() + "年" + (now.getMonth() + 1) + "月" +
now.getDate() + "日" +"星期" + now.getDay();
return ( goodMorning +"\n" +"我们在一起的第" +days +"天" +"\n" + weather +"\n" rainbow_1 +"\n" +rainbow_2 +"\n" +mornin);
}
async function getRainbow_1() {
let { data } = await http.get("https://chp.shadiao.app/api.php");
return data;
}
async function getWeather() {
let { data } = await http.get("http://wthrcdn.etouch.cn/weather_mini", {params: { city: city },});
return ("天气:" + data.data.forecast[0].type + ";温度:" + data.data.wendu + "℃" );
}
async function getRainbow_2() {
let { data } = await http.get("http://api.tianapi.com/txapi/tiangou/index", {
params: { key: key },
});
return data.newslist[0].content;
}
async function goodNight() {
let { data } = await http.get("http://api.tianapi.com/txapi/wanan/index", {
params: { key: key },
});
let rainbow_2 = await getRainbow_2();
let goodNight = data.newslist[0].content;
return rainbow_2 + "\n" + goodNight;
}
async function emitEvent() {
let { data } = await http.get("http://api.tianapi.com/txapi/worldtime/index",{ params: { key: key, city: "上海" } });
let time = new Date(data.newslist[0].strtime);
let now_time = time.getHours();
if (now_time == 8) {
email_details = await goodMorning();
} else if (now_time == 23) {
email_details = await goodNight();
} else {
email_details = "爱你每一天";
}
return email_details;
}
async function send_email() {
let email_details = await emitEvent();
let transporter = nodemailer.createTransport({
service: "qq",
port: 465, // SMTP 端口
secureConnection: true,
auth: {
user: email_user,
pass: email_password,
},
});
let mailOptions = {
from: email_user, // sender address
to: addressee,
subject: "日记",
text: email_details,
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log("Message sent: %s", info.messageId);
});
}
exports.main_handler = () => {
return send_email();
};
四、万事具备,只差Serverless
1.注册登录腾讯云(cloud.tencent.com/)
2.打开腾讯云控制台(console.cloud.tencent.com/)
3.打开云函数菜单,依次选择函数服务->地区(随便选个)->新建
4.新建空白node函数
5.新建文件 命名为package.json,并复制一下内容覆盖
{
"name": "bot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.0",
"nodemailer": "^6.4.16"
}
}
6.将上述代码覆盖到index.js选择在线安装依赖,点击保存
7.定时触发 点击左侧触发管理,进行设置,我设置的 早上8点 和晚上11点触发
cron表达式为 0 0 8 * * * * 0 0 23 * * * *
五、结果。
六、总结
以上,我们简单实现了一个彩虹屁bot,
再也不用担心忘记跟女票早午晚安了,哈哈哈哈哈哈哈哈哈哈哈嗝~
PS: 以上情节纯属虚构,文章有借鉴掘金大佬的python版本