二维码
搬瓦工 locvps

准备工作: 

目前最简单最好用的微信推送方式

企业微信注册免费https://work.weixin.qq.com
注册后,创建个群,然后添加机器人进去,取到webhook地址,然后按文档POST提交要提醒的数据就行了。
官方文档

  1. https://developer.work.weixin.qq.com/document/path/99110

查询余额达到设定的阈值后微信通知。

  1. functiON sendCurlRequest($url, $headers, $data) {


  2.     $ch= curl_init();

  3.     curl_setopt($ch, CURLOPT_URL, $url);

  4.     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

  5.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  6.     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

  7.     curl_setopt($ch, CURLOPT_POST, true);

  8.     $response = curl_exec($ch);

  9.     curl_close($ch);

  10.     return $response;

  11. }


  12. $apiKey = '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';

  13. $headers = array( 'Authorization: Bearer ' . $apiKey, 'Content-Type: application/json');

  14. $url = "https://api.openai.com/v1/dashboard/billing/usage?start_date=".date('Y-m-01')."&end_date=".date('Y-m-t');

  15. $response = sendCurlRequest($url, $headers, $data);

  16. $max_usage =100;//通知触发阈值

  17. if(strstr($response,'error')){

  18.         echo "invalid_api_key";}else{

  19.         $cost_data=json_decode($response);

  20.         $total_usage=round($cost_data->total_usage/100,2);

  21. }

  22. if( $total_usage>$max_usage){

  23. $url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa';

  24. $headers = array('Content-Type: application/json');

  25. $data = array("msgtype" => "text", "text" => array("content" => "余额不足","mentioned_list" => array("mjj","@all")));

  26. $response = sendCurlRequest($url, $headers, $data);

  27. echo "余额不足";

  28. }

说明:保存为notify.php,每小时访问一次,mentioned_list为通知的微信ID列表。


  1. 使用以下命令编辑cron任务列表:


  2. crontab -e

  3. 在打开的文件中,添加以下行来设置每小时执行的cron任务:


  4. 0 * * * * curl http://127.0.0.1/notify.php

locvps
评论专区
  • 昵 称必填
  • 邮 箱必填
  • 网 址选填
◎已有 0 人评论
BudgetVM
作者介绍
总干事

总干事

总干事作为本站编辑,主要负责VPS优惠、域名优惠和站长百科三个栏目。
80vps henghost
budgetvm sugarhosts
二维码

分享:

支付宝

微信