Python 透過 LINE Notify 與 MQTT--> Node-red LINE Notify傳送雷達回波圖
源自於https://steam.oxxostudio.tw/category/python/spider/radar.html
import requests
import paho.mqtt.client as mqtt #import the client1
import time
publish=''
def on_message(client, userdata, message):
print("message received " ,str(message.payload.decode("utf-8")))
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
########################################
broker_address="broker.mqtt-dashboard.com"
#broker_address="iot.eclipse.org"
print("creating new instance")
client = mqtt.Client("P1") #create new instance
client.on_message=on_message #attach function to callback
print("connecting to broker --> ",broker_address )
client.connect(broker_address) #connect to broke
########################################
radar_url = 'https://opendata.cwb.gov.tw/fileapi/v1/opendataapi/O-A0058-003?Authorization=rdec-key-123-45678-011121314&format=JSON'
radar = requests.get(radar_url) # 爬取資料
radar_json = radar.json() # 使用 JSON 格式
radar_img = radar_json['cwbopendata']['dataset']['resource']['uri'] # 取得圖片網址
print(radar_img)
'''
msg.headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer A4wwPNh2WqB7dlfeQyyIAwtggn1kfZSI5LkkCdia1gB'};
msg.payload = {"message":msg.payload};
return msg;
'''
url = 'https://notify-api.line.me/api/notify' # LINE Notify API 網址
#token = '你的 LINE Notify 權杖' # 自己申請的 LINE Notify 權杖
token = 'A4wwPNh2WqB7dlfeQyyIAwtggn1kfZSI5LkkCdia1gB'
headers = {
'Authorization': 'Bearer ' + token # POST 使用的 headers
}
data = {
'message':'Python送出 從雷達回波看看會不會下雨~', # 發送的訊息
'imageThumbnail':radar_img, # 預覽圖網址
'imageFullsize':radar_img # 完整圖片網址
}
data = requests.post(url, headers=headers, data=data) # 發送 LINE NOtify
publish=radar_img
client.loop_start() #start the loop
client.publish("alex9ufo/weather/O-A0058-003",publish)
time.sleep(10) # wait
client.loop_stop() #stop the loop
>>> %Run weather_6.py
creating new instance
connecting to broker --> broker.mqtt-dashboard.com
https://cwbopendata.s3.ap-northeast-1.amazonaws.com/MSC/O-A0058-003.png
>>>
[{"id":"9829271aabbe6547","type":"http request","z":"1257c22baca94adc","name":"","method":"GET","ret":"bin","paytoqs":"ignore","url":"https://cwbopendata.s3.ap-northeast-1.amazonaws.com/MSC/O-A0058-003.png","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":260,"y":200,"wires":[["4bdb85e00d197670"]]},{"id":"e7765b8d9c73972d","type":"inject","z":"1257c22baca94adc","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":110,"y":240,"wires":[["9829271aabbe6547"]]},{"id":"4bdb85e00d197670","type":"base64","z":"1257c22baca94adc","name":"","action":"","property":"payload","x":420,"y":200,"wires":[["5efea32a70b22095","4416caaaf0b9114e"]]},{"id":"5efea32a70b22095","type":"image","z":"1257c22baca94adc","name":"","width":160,"data":"payload","dataType":"msg","thumbnail":true,"active":true,"outputs":0,"x":620,"y":200,"wires":[]},{"id":"047993ea0d909a53","type":"mqtt in","z":"1257c22baca94adc","name":"","topic":"alex9ufo/weather/O-A0058-003","qos":"2","datatype":"auto-detect","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":170,"y":120,"wires":[["8936ddaa7a14dd29","9829271aabbe6547"]]},{"id":"8936ddaa7a14dd29","type":"debug","z":"1257c22baca94adc","name":"debug 57","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":460,"y":120,"wires":[]},{"id":"4416caaaf0b9114e","type":"delay","z":"1257c22baca94adc","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":260,"y":300,"wires":[["10685170bcc4d2b2"]]},{"id":"10685170bcc4d2b2","type":"line-notify","z":"1257c22baca94adc","name":"","message":"send image 從Node-Red","contentType":"imageUrl","imageThumbnail":"","imageUrl":"https://cwbopendata.s3.ap-northeast-1.amazonaws.com/MSC/O-A0058-003.png","sticker":"default","stickerPackageId":"1","stickerId":"1","silent":false,"creds":"4d14683c16fc0999","x":400,"y":300,"wires":[["af93dbe2dfce13e9"]]},{"id":"af93dbe2dfce13e9","type":"image","z":"1257c22baca94adc","name":"","width":160,"data":"payload","dataType":"msg","thumbnail":true,"active":true,"outputs":0,"x":580,"y":360,"wires":[]},{"id":"841df58d.ee5e98","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"4d14683c16fc0999","type":"linetoken","name":"alex line"}]
沒有留言:
張貼留言