换成企业微信版本
[Python] 纯文本查看 复制代码 import requests
import time
import hmac
import hashlib
import base64
import json
def get_weather(city):
api_key = 'xxxxxxxx' # 替换为您的心知天气 API 密钥
url = f'https://api.seniverse.com/v3/weather/now.json?key={api_key}&location={city}&language=zh-Hans'
response = requests.get(url)
print(f"Requesting weather data with URL: {url}")
if response.status_code != 200:
print(f"Failed to get weather data: {response.status_code} - {response.text}")
return None
data = response.json()
if 'results' in data and data['results']:
weather_info = data['results'][0]['now']
weather_desc = weather_info['text']
weather_icon = get_weather_icon(weather_desc)
temperature = weather_info['temperature']
return weather_desc, weather_icon, temperature
else:
print("No results found in weather data")
return None
def get_weather_forecast(city):
api_key = 'xxxxxxx' # 替换为您的心知天气 API 密钥
url = f'https://api.seniverse.com/v3/weather/daily.json?key={api_key}&location={city}&language=zh-Hans&days=3'
response = requests.get(url)
print(f"Requesting weather forecast data with URL: {url}")
if response.status_code != 200:
print(f"Failed to get weather forecast data: {response.status_code} - {response.text}")
return None
data = response.json()
if 'results' in data and data['results']:
forecasts = data['results'][0]['daily']
return forecasts
else:
print("No results found in weather forecast data")
return None
def get_weather_icon(weather_desc):
if '晴' in weather_desc:
return '☀️'
elif '多云' in weather_desc:
return '☁️'
elif '雨' in weather_desc:
return '🌧️'
else:
return ''
def send_to_wechat(message, webhook_url):
headers = {'Content-Type': 'application/json'}
data = {
'msgtype': 'markdown',
'markdown': {
'content': message
}
}
response = requests.post(webhook_url, data=json.dumps(data), headers=headers)
if response.status_code == 200:
print("Message sent successfully to WeChat!")
else:
print(f"Failed to send message to WeChat: {response.status_code}")
# 直接指定城市名称
city = '北京'
# 从环境变量中获取企业微信机器人的 Webhook URL
webhook_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxx' # 替换为您的企业微信机器人的 Webhook URL
# 获取天气信息
weather_data = get_weather(city)
forecasts = get_weather_forecast(city)
# 构建消息内容
if weather_data and forecasts:
weather_desc, weather_icon, temperature = weather_data
message = f"### 今日天气提醒\n\n城市:{city}\n天气:{weather_icon} {weather_desc}\n温度:{temperature}°C\n\n【未来3天天气预报】\n\n"
for forecast in forecasts:
date = forecast['date']
weather_desc = forecast['text_day']
temperature_high = forecast['high']
temperature_low = forecast['low']
message += f"{date}: {weather_desc}, 温度:{temperature_low}°C ~ {temperature_high}°C\n\n"
else:
message = "未能获取到天气信息,请检查城市名称。"
# 发送消息到企业微信机器人
send_to_wechat(message, webhook_url)
|