15098017803 发表于 2022-11-13 13:27

【修改版】华为云函数实现每日天气推送

本帖最后由 15098017803 于 2022-11-13 13:40 编辑

原帖子地址:https://www.52pojie.cn/thread-1423918-1-1.html
原帖作者:@医生
和风天气:https://www.qweather.com/qmsg酱:https://qmsg.zendee.cn/
华为函数工作流:https://console.huaweicloud.com/functiongraph/
城市代码查询入口:https://where.heweather.com/index.html

以前抄过大佬的作业,昨天想起来这个,打算再抄一次,发现腾讯云函数需要小钱钱了,价格还不便宜,算了算了,于是换到了华为云(阿里云似乎也要收费?)。
把代码直接复制过去,发现从头到尾,全部运行不了报错,不会代码,零零散散百度了几个小时,算是能用了(本人关于代码的经验只有大学期间的vb2级)
发现def函数报错,不知道怎么处理(主要时间花在了这儿,搞不懂),删了,总之没报错
和风天气api更改了,重新修正
1、创建函数-创建空白函数-事件函数-命名-选择python3.6

2、删除index.py所有内容,讲以下代码复制进去
# -*- coding:utf-8 -*-
import requests
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning
# 禁用安全请求警告
#requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
def handler(event, context):
    yburl = 'https://api.qweather.com/v7/grid-weather/3d?' #格点天气3天
    value = {
      'location': '【你的地点】',#广州
      'key': '【你的key】',
      'lang': 'zh'
    }
    ybreq = requests.get(yburl, params=value)
    ybjs = ybreq.json()
    ybur2 = 'https://api.qweather.com/v7/weather/3d?' #城市天气3天
    value = {
      'location': '【你的地点】',
      'key': '【你的key】',
      'lang': 'zh'
    }
    ybreq2 = requests.get(ybur2, params=value)
    ybjs2 = ybreq2.json()
    # 返回api参数:
    # print(ybjs)
    for i in range(2):
      yb = ybjs['daily']
      yb2 = ybjs2['daily']
      d1 = yb['fxDate'] + '\n气温: ' + yb['tempMin'] + '—' + yb['tempMax'] + '℃'+ '    降雨量:' +yb['precip']   + '   \n日间:' + yb['textDay']   + '' + yb['windDirDay']+ ' ' + yb['windScaleDay'] + '级' + '日出:' + yb2['sunrise'] + ' 日落:' + yb2['sunset'] + '   \n夜间:' + yb['textNight'] + ' ' + ' ' + yb['windDirNight']+ ' ' + yb['windScaleNight'] + '级' + '月升:' + yb2['moonrise'] + ' 月落:' + yb2['moonset'] + '   ' +yb2['moonPhase']
      # output(d1)
    # qq推送
    qqtalk = 'https://qmsg.zendee.cn/send/【你的qsmgkey】?msg=' + d1 +'&qq=【你的qq】'
    requests.get(qqtalk)

3、选择部署——测试(选择空白测试) qq收到了如下消息

4、创建触发器——定时触发器——cron表达式 【0 0 22 * * ?】 每晚10点




关于api,我同时使用了两个
格点天气:https://dev.qweather.com/docs/api/grid-weather/grid-weather-daily-forecast/
城市天气:https://dev.qweather.com/docs/api/weather/weather-daily-forecast/
原因是格点天气更加准确(大概),但是缺少了日出日落时间,所以额外加了个城市天气的api

api我是用的是收费版,但是由于是个人使用,预估一个月一毛钱都用不了


之前一直以为推送无法换行,发现\n 可以用于qq的换行,太棒了


qmsg的机器人我用的自己的一个小号挂载在服务器上的,他们的公共机器人似乎全部宕机了,
qmsg自己有教程(https://qmsg.zendee.cn/pvt)
我额外参照了这位大佬的博客:https://blog.panghai.top/posts/7b50/
同时大佬的机器人后台运行的代码似乎有问题,我用的是
nohup /www/wwwroot/QmsgClient2.1_20220916_1/run.sh
nohup 是相关命令
/www/wwwroot/QmsgClient2.1_20220916_1/ 是我的文件夹
run.sh 是官方建议的启动程序(大佬直接启动QmsgClient2.1.jar,我启动就卡住)
> Qmsg.log 2>&1 & 不懂,照抄

18809876635 发表于 2023-5-7 22:04

大佬你好,看见你最后部署qmsg酱,我使用服务器最后出现下面代码这种情况是什么原因,感谢大佬指导,我完全按照您帖子里的提到的博客步骤部署的13:55:35,106 |-INFO in ch.qos.logback.classic.LoggerContext - Could NOT find resource
13:55:35,106 |-INFO in ch.qos.logback.classic.LoggerContext - Could NOT find resource
13:55:35,106 |-INFO in ch.qos.logback.classic.LoggerContext - Found resource at
13:55:35,130 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@7cdbc5d3 - URL is not of type file
13:55:35,194 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
13:55:35,196 |-INFO in ch.qos.logback.classic.joran.action.ContextNameAction - Setting logger context name as
13:55:35,197 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type
13:55:35,200 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as
13:55:35,214 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type for property
13:55:35,260 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type
13:55:35,263 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as
13:55:35,266 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type for property
13:55:35,295 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@984213526 - No compression will be used
13:55:35,296 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@984213526 - Will use the pattern ./logs/debug/log-debug-%d{yyyy-MM-dd}.%i.log for the active file
13:55:35,298 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@17d99928 - The date pattern is 'yyyy-MM-dd' from file name pattern './logs/debug/log-debug-%d{yyyy-MM-dd}.%i.log'.
13:55:35,298 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@17d99928 - Roll-over at midnight.
13:55:35,306 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@17d99928 - Setting initial period to Sun May 07 13:41:44 UTC 2023
13:55:35,306 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@17d99928 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
13:55:35,306 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@17d99928 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
13:55:35,317 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender - Active log file name: ./logs/log_debug.log
13:55:35,317 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender - File property is set to [./logs/log_debug.log]
13:55:35,317 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type
13:55:35,317 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as
13:55:35,317 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type for property
13:55:35,322 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@942986815 - No compression will be used
13:55:35,322 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@942986815 - Will use the pattern ./logs/info/log-info-%d{yyyy-MM-dd}.%i.log for the active file
13:55:35,324 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ae369b7 - The date pattern is 'yyyy-MM-dd' from file name pattern './logs/info/log-info-%d{yyyy-MM-dd}.%i.log'.
13:55:35,324 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ae369b7 - Roll-over at midnight.
13:55:35,325 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ae369b7 - Setting initial period to Sun May 07 13:53:47 UTC 2023
13:55:35,326 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ae369b7 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
13:55:35,326 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ae369b7 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
13:55:35,329 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender - Active log file name: ./logs/log_info.log
13:55:35,329 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender - File property is set to [./logs/log_info.log]
13:55:35,329 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type
13:55:35,329 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as
13:55:35,330 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type for property
13:55:35,338 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1879034789 - No compression will be used
13:55:35,338 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1879034789 - Will use the pattern ./logs/warn/log-warn-%d{yyyy-MM-dd}.%i.log for the active file
13:55:35,339 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@34340fab - The date pattern is 'yyyy-MM-dd' from file name pattern './logs/warn/log-warn-%d{yyyy-MM-dd}.%i.log'.
13:55:35,339 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@34340fab - Roll-over at midnight.
13:55:35,339 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@34340fab - Setting initial period to Sun May 07 13:41:45 UTC 2023
13:55:35,339 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@34340fab - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
13:55:35,339 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@34340fab - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
13:55:35,342 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender - Active log file name: ./logs/log_warn.log
13:55:35,342 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender - File property is set to [./logs/log_warn.log]
13:55:35,342 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type
13:55:35,342 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as
13:55:35,343 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type for property
13:55:35,348 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@716157500 - No compression will be used
13:55:35,348 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@716157500 - Will use the pattern ./logs/error/log-error-%d{yyyy-MM-dd}.%i.log for the active file
13:55:35,348 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2b80d80f - The date pattern is 'yyyy-MM-dd' from file name pattern './logs/error/log-error-%d{yyyy-MM-dd}.%i.log'.
13:55:35,348 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2b80d80f - Roll-over at midnight.
13:55:35,349 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2b80d80f - Setting initial period to Sun May 07 13:41:45 UTC 2023
13:55:35,349 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2b80d80f - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
13:55:35,349 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2b80d80f - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
13:55:35,349 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender - Active log file name: ./logs/log_error.log
13:55:35,349 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender - File property is set to [./logs/log_error.log]
13:55:35,350 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger to OFF
13:55:35,350 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
13:55:35,350 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named to Logger
13:55:35,351 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named to Logger
13:55:35,351 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
13:55:35,351 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named to Logger
13:55:35,351 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named to Logger
13:55:35,351 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named to Logger
13:55:35,351 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
13:55:35,356 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@3ab39c39 - Registering current configuration as safe fallback point

2023-05-07 13:55:35,669 INFOc.I.Y.d.ConfigManagerDefaultImpl@ - ConfigManager Init.
2023-05-07 13:55:35,820 INFOc.I.Y.d.ConfigManagerDefaultImpl@ - ConfigManager Config Mode: dev.
2023-05-07 13:55:35,823 INFOc.I.Y.d.ConfigManagerDefaultImpl@ - ConfigManager Init Success.
2023-05-07 13:55:38,630 INFOc.I.Y.a.AsLoader@ - Init ApplicationService: EhcacheHelpFactory Success!
2023-05-07 13:55:38,631 INFOc.I.Y.a.AsLoader@ - Init ApplicationService: JobManagerImpl Success!
2023-05-07 13:55:38,633 INFOc.i.y.a.ArtQQBot@ - Load By UIF: /root/.yuq/.art/.device/info_2232196592.json
2023-05-07 13:55:38,672 INFOo.a.c.EcdhCrypt@ - setPubKey 0440eaf325b9c66225143aa7f3961c953c3d5a8048c2b73293cdc7dcbab7f35c4c66aa8917a8fd511f9d969d02c8501bcaa3e3b11746f00567e3aea303ac5f2d25 ver:2:
2023-05-07 13:55:38,676 INFOo.a.c.EcdhCrypt@ - initShareKeyByBouncycastle failed, :
initShareKeyByDefault
2023-05-07 13:55:38,676 INFOo.a.c.EcdhCrypt@ - initShareKeyByDefault OK:
Begin loginning.
java.lang.NullPointerException
      at com.icecreamqaq.yuq.artqq.protocol.login.WtLogin$Login.decode(WtLogin.kt:79)
      at com.icecreamqaq.yuq.artqq.protocol.login.WtLogin$Login.decode(WtLogin.kt:64)
      at com.icecreamqaq.yuq.artqq.ArtQQBot$YuQHandler.handlePacket(ArtQQBot.kt:1345)
      at org.artqq.ReceiveData.parseMessage(ReceiveData.java:112)
      at org.artqq.ReceiveData$BuiltThread.run(ReceiveData.java:962)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:750)
Exception in thread "pool-2-thread-1" java.lang.RuntimeException: wait packet: wtlogin.login timeout!
      at org.artqq.ReceiveData$HandlePacket.waitPacket(ReceiveData.java:1043)
      at org.artqq.Wtlogin._Login.sendWtLogin(_Login.java:64)
      at org.artqq.Wtlogin._Login.run(_Login.java:34)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:750)

15098017803 发表于 2022-11-15 10:02

我今天是大佬 发表于 2022-11-14 09:55
腾讯云函数真的贼贵, 我之前就print一下, 试用忘记关了. 一个月就扣了10几块钱

话说华为云收费吗

腾讯云函数今年开始,基础套餐一个月要扣12元吧好像,华为云不收费,前10w次?还是100w次?

勤快的懒虫 发表于 2022-11-13 15:49

华为里面的权限不足

518pj 发表于 2022-11-13 18:29

学习了,谢谢大佬

Sp1ral 发表于 2022-11-13 18:30

学到了学到了{:1_893:}

wg198300 发表于 2022-11-13 18:43

华为免费吗?

tongyan 发表于 2022-11-13 19:06

感谢分享

qfxldhw 发表于 2022-11-13 21:19


学习了,谢谢大佬

xiadongming 发表于 2022-11-13 22:24

AlwaysJms 发表于 2022-11-14 09:01

学习一下,感谢感谢{:1_893:}

丶MaGuoGuo 发表于 2022-11-14 09:40

感谢感谢 非常有用的知识
页: [1] 2
查看完整版本: 【修改版】华为云函数实现每日天气推送