python使用微信推送訊息

2022-01-12 20:39:27 字數 4245 閱讀 1923

from wxpy import *   #

import

requests

from datetime import

datetime

import

time

import

schedule

from apscheduler.schedulers.blocking import blockingscheduler #

定時框架

import

schedule

#bot = bot(cache_path=true)##

然後我們寫乙個變數,用來存在圖靈機械人的key,後面會用到(去圖靈申請乙個免費的機械人即可)

tuling = tuling(api_key="

41648a0b8ed94a50ac08fb1cc7b5d1dd")

"""推送名單維護

"""def weather_friend(): #

定義乙個名為weather_friend的函式

#天氣推送名單 也可以送備註來搜尋好友

my_friends = #

建立乙個空列表,用來存放好友名單

相忘於江湖

')[0]) #

搜尋指定好友並新增至列表##

return

my_friends

# 傳送的內容

def weather(location): #

#準備url位址

path ='

'url = path %location

response = requests.get(url) #

對該位址和引數進行get請求

result = response.json() #

將返回的結果轉成json串

#為了防止因好友未設定地點而導致程式報錯,所以需要對返回的error引數進行判斷

if int(result['

error

']) != 0: #

當error為0時,搜尋是正常的,一旦不等於0,表示存在錯誤

location = '北京'

#此時將位址設為乙個預設位址 如北京

url = path % location #

拼接新的url

response = requests.get(url) #

在對新的url進行get請求

result =response.json()

#下面就是對正確請求到資料後的結果進行處理了

#問候語

greetings = ('

早上好!這是今天的天氣預報!……\n機械人:你的小野貓\n')

try: #

此處增加異常處理是因為當好友設定的地區為國外的時候,error返回值不會報錯,但不會有results

#取出天氣結果

results = result['

results']

except

:

return

"啊哦,我迷路了,找不到地點!"#

當沒有該地區的結果時,函式直接返回字串,此時函式結束

#當正常取出結果後,繼續進行下列操作

#取出資料字典第一天資料

data1 =results[0]

#取出城市

city = data1['

currentcity']

str1 = '

你的城市: %s\n

' %city

#取出pm2.5值

pm25 = data1['

pm25']

str2 = '

pm值: %s\n

' %pm25

#評估空氣質素

pollution = calculate(pm25) #

此時呼叫calculate函式進行計算,所以該函式要寫在此函式之前

str3 = '

汙染指數: %s\n

' %pollution

result1 =results[0]

weather_data = result1['

weather_data']

data =weather_data[0]

temperature_now = data['

date']

str4 = '

當前溫度: %s\n

' %temperature_now

wind = data['

wind']

str5 = '

風向 : %s\n

' %wind

weather = data['

weather']

str6 = '

天氣 : %s\n

' %weather

str7 = '

溫度 : %s\n

' % data['

temperature']

try: #

此處異常操作是因為有時候返回結果裡面不存在下方內容

message = data1['

index']

str8 = '

穿衣 : %s\n

' % message[0]['

des'

] str9 = '

我很貼心: %s\n

' % message[2]['

des'

] str10 = '

運動 : %s\n

' % message[3]['

des'

] str11 = '

紫外線 : %s\n

' % message[4]['

des'

] str = greetings + str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8 + str9 + str10 +str11

except

: str = greetings + str1 + str2 + str3 + str4 + str5 + str6 +str7

return

str#

計算pm2.5的程度

defcalculate(pm):

if pm == ''

: pm25 = -1

else

: pm25 =int(pm)

#通過pm2.5的值大小判斷汙染指數

if 0 <= pm25 < 35:

pollution = '優'

elif 35 <= pm25 < 75:

pollution = '良'

elif 75 <= pm25 < 115:

pollution = '

輕度汙染

'elif 115 <= pm25 < 150:

pollution = '

中度汙染

'elif 150 <= pm25 < 250:

pollution = '

重度汙染

'elif pm25 >= 250:

pollution = '

嚴重汙染

'else

: pollution = '

希望你健在

'return

pollution

#1- 天氣推送

defsend_weather():

my_friends =weather_friend()

#傳送給好友

for friend in

my_friends:

friend.send(weather(friend.city)) ##

給檔案傳輸助手傳送訊息

bot.file_helper.send('

天氣推送完畢')

"""推送時間維護

"""print('

star')

sched =blockingscheduler() # 定時框架

sched.add_job(send_weather,

'cron

', month='

1-12

', day='

1-31

', hour=21, minute=12) #

表示每天的21點12分傳送

sched.start() # 服務啟動

感謝趙先森~!

結束!

python使用微信推送訊息

from wxpy import import requests from datetime import datetime import time import schedule from apscheduler.schedulers.blocking import blockingschedul...

微信模版訊息推送

class weixinpush 傳送post請求 param string url param string param return bool mixed function request post url param posturl url curlpost param ch curl ini...

企業微信訊息推送

獲取token admin admin adminservice.getcurrent crop co cropservice.findbyadmin admin.getid stringbuffer str newstringbuffer co.setsecret co.getsecret str...