先看效果¶
教程开始¶
步骤1¶
我们打开金山文档新建一个智能表格
步骤2¶
按照表格内容填入信息
| 企业微信推送Webhook | 填入企业微信群聊机器人webhook | 获取webhook教程:https://developer.qiniu.com/console/kb/10490/receptionmanagement-wechat?category=kb | ||
|---|---|---|---|---|
| 和风天气Key | 填入key | api key申请地址:https://dev.qweather.com/ | ||
| 彩虹屁Key | 填入key | 彩虹屁 key申请地址:彩虹屁API接口 - 天聚数行TianAPI | ||
| 纪念日 | 2024-05-30 | 填写某个纪念日日期 | ||
| 深圳-南山 | 是 | 是:开启,否:关闭 | ||
| 广西-南宁 | 是 |
或者一键另存为自己的wps云文档【金山文档 | WPS云文档】 早安(副本)
https://kdocs.cn/l/cd2R2Nc3Xjnz
步骤3¶
按下图找到AirScript脚本编辑器
步骤4¶
填入代码
/**
* 说明:本脚本功能为获取天气信息(含穿衣建议)和彩虹屁,并通过企业微信机器人推送给用户
*/
function main() {
const sheet = Application.ActiveSheet;
const myDate = new Date();
const dateStr = `${myDate.getFullYear()}/${(myDate.getMonth() + 1).toString().padStart(2, '0')}/${myDate.getDate().toString().padStart(2, '0')}`;
const WEBHOOK_URL = sheet.Range("B1").Text;
const QWEATHER_KEY = sheet.Range("B2").Text;
const TIANAPI_KEY = sheet.Range("B3").Text;
const LOVE_DATE = sheet.Range("B4").Text;
const cityList = getCityList(sheet);
const weatherMsg = getCityWeather(cityList, QWEATHER_KEY);
const rainbowMsg = getRainbowMessage(TIANAPI_KEY);
const loveDaysMsg = getLoveDays(LOVE_DATE);
let finalMessage = `📅 亲爱的,今天是 ${dateStr},${loveDaysMsg}\n\n`;
if (weatherMsg) finalMessage += weatherMsg + "\n\n";
if (rainbowMsg) finalMessage += rainbowMsg;
sendWeChatMessage(WEBHOOK_URL, finalMessage);
}
/**
* 计算纪念日天数
*/
function getLoveDays(loveDateStr) {
if (!loveDateStr) return "💖 我们领证的第 0 天 💖";
try {
const loveDate = new Date(loveDateStr);
const today = new Date();
const diffTime = today - loveDate;
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
return `💖 我们领证的第 ${diffDays} 天 💖`;
} catch (error) {
console.log("计算纪念日天数出错:", error.message);
return "💖 纪念日计算错误 💖";
}
}
/**
* 获取城市列表
*/
function getCityList(sheet) {
const cities = [];
let row = 5; // 从第5行开始存储城市信息
while (true) {
const cityName = sheet.Range(`A${row}`).Text;
if (!cityName) break;
if (sheet.Range(`B${row}`).Text === '是') {
cities.push(cityName);
}
row++;
}
return cities;
}
/**
* 获取天气信息
*/
function getCityWeather(cityList, apiKey) {
const weatherInfo = [];
cityList.forEach(cityName => {
try {
const cityData = getCityId(cityName, apiKey);
if (!cityData) return;
const cityId = cityData.id;
const weatherData = getWeatherData(cityId, apiKey);
const lifeAdvice = getLifeAdvice(cityId, apiKey);
if (weatherData) {
const weatherIcon = getWeatherIcon(weatherData.textDay);
weatherInfo.push(`${weatherIcon} ${cityName} ${weatherData.textDay},${weatherData.tempMin}~${weatherData.tempMax}℃`);
}
if (lifeAdvice) {
weatherInfo.push("👕 " + lifeAdvice);
}
} catch (error) {
console.log(`获取${cityName}天气数据失败:`, error.message);
}
});
return weatherInfo.length ? weatherInfo.join("\n") : null;
}
/**
* 获取城市ID
*/
function getCityId(cityName, apiKey) {
const url = `https://geoapi.qweather.com/v2/city/lookup?location=${cityName}&key=${apiKey}`;
const response = HTTP.get(url);
const data = JSON.parse(response.text());
if (data.code === "200" && data.location.length > 0) {
return data.location[0];
}
console.log(`没有找到 ${cityName}`);
return null;
}
/**
* 获取天气数据
*/
function getWeatherData(cityId, apiKey) {
const url = `https://devapi.qweather.com/v7/weather/3d?location=${cityId}&key=${apiKey}`;
const response = HTTP.get(url);
const data = JSON.parse(response.text());
if (data.code === "200") {
return data.daily[0];
}
return null;
}
/**
* 获取穿衣建议
*/
function getLifeAdvice(cityId, apiKey) {
const url = `https://devapi.qweather.com/v7/indices/1d?type=3&location=${cityId}&key=${apiKey}`;
const response = HTTP.get(url);
const data = JSON.parse(response.text());
if (data.code === "200" && data.daily.length > 0) {
return data.daily[0].text;
}
return null;
}
/**
* 获取天气图标
*/
function getWeatherIcon(text) {
const weatherMap = {
"晴": "☀️",
"阴": "☁️",
"云": "⛅️",
"雪": "☃️",
"雷": "⛈️",
"沙": "🏜️",
"尘": "🏜️",
"雾": "🌫️",
"霾": "🌫️",
"风": "🌪️",
"雨": "🌧️"
};
for (const [type, icon] of Object.entries(weatherMap)) {
if (text.includes(type)) {
return icon;
}
}
return "🌤️";
}
/**
* 获取彩虹屁消息
*/
function getRainbowMessage(apiKey) {
try {
const url = `http://api.tianapi.com/caihongpi/index?key=${apiKey}`;
const response = HTTP.get(url);
const data = JSON.parse(response.text());
if (data.code === 200) {
return "🌈 " + data.newslist[0].content;
}
console.log("获取彩虹屁失败:", data.msg);
return null;
} catch (error) {
console.log("获取彩虹屁异常:", error.message);
return null;
}
}
/**
* 发送企业微信消息
*/
function sendWeChatMessage(webhookUrl, message) {
try {
const response = HTTP.post(webhookUrl, {
msgtype: "text",
text: {
content: message
}
});
const result = JSON.parse(response.text());
if (result.errcode !== 0) {
console.log("发送消息失败:", result.errmsg);
}
} catch (error) {
console.log("发送消息异常:", error.message);
}
}
// 执行主函数
main();
启用网络api服务
步骤5¶
保存运行调试,并开启每天定时运行
技术实现其实不难,对于广大同志们而言,难的是:
部分图片和教程参考:用AirScript脚本给女/男朋友发送每日早安邮件(极简版本) - Wcowin's Blog
但是代码全是claude写的 ![]()










