用一个微信测试号和node写了个定时天气预报

240 阅读1分钟

微信公众号要实现定时发消息,只能是认证的服务号,因为发送模板消息目前微信只对认证的服务号开放

申请测试号

为什么是测试号呢? 申请地址 

如果你本地测试就不需要输入对应的服务器Url、token

image.png

然后扫描二维码,关注测试号,然后要对服务号随便发一条消息,如果不发,到时接口会提示报错

image.png

/*
 * @Descripttion: 
 * @Author: lihk
 * @Date: 2024-03-16 15:21:05
 */

import axios from "axios";

// 微信公众号的 AppID 和 AppSecret
const APPID = '你的appId';
const APPSECRET = '你的appsecret';

// 获取 access_token 的 URL
const accessTokenUrl = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${APPID}&secret=${APPSECRET}`;

// 发送消息的函数
async function sendWechatMessage (accessToken, weatherData, user_id) {
  console.log("🚀 ~ sendWechatMessage ~ weatherData:", weatherData)
  const url = `https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${accessToken}`;
  const data = {
    // touser: '上面二维码的用户的微信号', // 用户的 OpenID
    touser: user_id, // 用户的 OpenID
    template_id: '新增的模板的Id', // 模板ID
    data: {
      // 根据您的模板内容,这里填写对应的字段
      date: {
        value: weatherData.date, // 使用 weatherData 中的日期
        color: "#173177"
      },
      weather: {
        value: weatherData.weather, // 使用 weatherData 中的天气情况
        color: "#173177"
      },
      temperature: {
        value: weatherData.temperature, // 使用 weatherData 中的温度范围
        color: "#173177"
      },
      tip: {
        value: weatherData.tip, // 使用 weatherData 中的提示信息
        color: "#FF4500"
      },
      // ...其他模板数据
    }
  };
  try {
    const response = await axios.post(url, data);
    console.log('消息发送结果:', response.data);
  } catch (error) {
    console.error('发送消息失败:', error);
  }
}
function calculateDaysSince (dateString) {
  const startDate = new Date(dateString);
  const currentDate = new Date();

  // 计算两个日期之间的时间差(以毫秒为单位)
  const timeDifference = currentDate - startDate;

  // 将时间差转换为天数
  const daysDifference = timeDifference / (1000 * 60 * 60 * 24);

  // 返回天数差的绝对值并向下取整
  return Math.floor(Math.abs(daysDifference));
}

export {
  sendWechatMessage,
  accessTokenUrl,
  calculateDaysSince
}

新增的模板,每一个字段都要DATA结尾,然后最后面最好加上一个符号,不然会导致最后一句不显示

今天是:{{date.DATA}} 
天气情况:{{weather.DATA}} 
温度:{{temperature.DATA}} 
温馨提示:{{tip.DATA}} !

在app.js里面调用这个js文件的代码

// 获取token
const response = await axios.get(accessTokenUrl);
const accessToken = response.data.access_token;
// 要传的字段
const weatherData = {
        date: new Date().toLocaleDateString() + ' - ' + weaterResponse.week,
        weather: `${weaterResponse.city}-天气${weaterResponse.wea}-${weaterResponse.win}${weaterResponse.win_speed}`,
        temperature: `${weaterResponse.tem_night}°C - ${weaterResponse.tem_day}°C`,
        tip: tipsMap[weaterResponse.wea], // 这里可以根据天气情况给出不同的提示,
      };
await sendWechatMessage(accessToken, weatherData, '二维码处的微信号');

我这里是使用免费的天气接口

const weaterApi = `http://v1.yiketianqi.com/free/day?appid=你申请的appId&appsecret=你申请的appsecret&unescape=1&city=城市`

然后在进行调用

const { data: weaterResponse } = await axios.get(weaterApi);

最终效果图

9dd1d10787d0b46dad5f62901a14929.jpg