mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2025-12-20 14:14:52 +01:00
various improvements and new plugins
This commit is contained in:
@@ -1,49 +1,49 @@
|
||||
import json
|
||||
from typing import Dict
|
||||
|
||||
import requests
|
||||
|
||||
from bot.plugins.plugin import Plugin
|
||||
|
||||
def weather_function_spec():
|
||||
return {
|
||||
"name": "get_current_weather",
|
||||
"description": "Get the current and 7-day daily weather forecast for a location using Open Meteo APIs.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"latitude": {
|
||||
"type": "string",
|
||||
"description": "Latitude of the location"
|
||||
|
||||
class WeatherPlugin(Plugin):
|
||||
"""
|
||||
A plugin to get the current weather and 7-day daily forecast for a location
|
||||
"""
|
||||
def get_source_name(self) -> str:
|
||||
return "OpenMeteo"
|
||||
|
||||
def get_spec(self) -> Dict:
|
||||
return {
|
||||
"name": "get_current_weather",
|
||||
"description": "Get the current and 7-day daily weather forecast for a location using Open Meteo APIs.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"latitude": {
|
||||
"type": "string",
|
||||
"description": "Latitude of the location"
|
||||
},
|
||||
"longitude": {
|
||||
"type": "string",
|
||||
"description": "Longitude of the location"
|
||||
},
|
||||
"unit": {
|
||||
"type": "string",
|
||||
"enum": ["celsius", "fahrenheit"],
|
||||
"description": "The temperature unit to use. Infer this from the provided location.",
|
||||
},
|
||||
},
|
||||
"longitude": {
|
||||
"type": "string",
|
||||
"description": "Longitude of the location"
|
||||
},
|
||||
"unit": {
|
||||
"type": "string",
|
||||
"enum": ["celsius", "fahrenheit"],
|
||||
"description": "The temperature unit to use. Infer this from the provided location.",
|
||||
},
|
||||
},
|
||||
"required": ["latitude", "longitude", "unit"],
|
||||
"required": ["latitude", "longitude", "unit"],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async def get_current_weather(latitude, longitude, unit):
|
||||
"""
|
||||
Get the current weather in a given location using the Open Meteo API
|
||||
Source: https://open-meteo.com/en/docs
|
||||
:param latitude: The latitude of the location to get the weather for
|
||||
:param longitude: The longitude of the location to get the weather for
|
||||
:param unit: The unit to use for the temperature (`celsius` or `fahrenheit`)
|
||||
:return: The JSON response to be fed back to the model
|
||||
"""
|
||||
request = requests.get(f'https://api.open-meteo.com/v1/forecast'
|
||||
f'?latitude={latitude}'
|
||||
f'&longitude={longitude}'
|
||||
f'&daily=weathercode,temperature_2m_max,temperature_2m_min,precipitation_probability_mean,'
|
||||
f'&forecast_days=7'
|
||||
f'&timezone=auto'
|
||||
f'&temperature_unit={unit}'
|
||||
f'¤t_weather=true')
|
||||
return json.dumps(request.json())
|
||||
async def execute(self, **kwargs) -> Dict:
|
||||
url = f'https://api.open-meteo.com/v1/forecast'\
|
||||
f'?latitude={kwargs["latitude"]}'\
|
||||
f'&longitude={kwargs["longitude"]}'\
|
||||
f'&temperature_unit={kwargs["unit"]}' \
|
||||
'¤t_weather=true' \
|
||||
'&daily=weathercode,temperature_2m_max,temperature_2m_min,precipitation_probability_mean,' \
|
||||
'&forecast_days=7' \
|
||||
'&timezone=auto'
|
||||
return requests.get(url).json()
|
||||
|
||||
Reference in New Issue
Block a user