small improvements to the worldtimeapi plugin

This commit is contained in:
ned
2023-07-02 20:48:12 +02:00
parent d29d3923fc
commit ff8101b26c
3 changed files with 33 additions and 41 deletions

View File

@@ -100,28 +100,28 @@ Check out the [Budget Manual](https://github.com/n3d1117/chatgpt-telegram-bot/di
#### Functions
| Parameter | Description | Default value |
|-----------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------|
|-----------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------|
| `ENABLE_FUNCTIONS` | Whether to use functions (aka plugins). You can read more about functions [here](https://openai.com/blog/function-calling-and-other-api-updates) | `true` (if available for the model) |
| `FUNCTIONS_MAX_CONSECUTIVE_CALLS` | Maximum number of back-to-back function calls to be made by the model in a single response before displaying a user-facing message | `10` |
| `PLUGINS` | List of plugins to enable (see below for a full list), e.g: `PLUGINS=wolfram,weather` | `-` |
| `PLUGINS` | List of plugins to enable (see below for a full list), e.g: `PLUGINS=wolfram,weather` | - |
| `SHOW_PLUGINS_USED` | Whether to show which plugins were used for a response | `false` |
| `WOLFRAM_APP_ID` | Wolfram Alpha APP ID (required for the `wolfram` plugin, you can get one [here](https://products.wolframalpha.com/simple-api/documentation)) | `-` |
| `SPOTIFY_CLIENT_ID` | Spotify app Client ID (required for the `spotify` plugin, you can find it on the [dashboard](https://developer.spotify.com/dashboard/)) | `-` |
| `SPOTIFY_CLIENT_SECRET` | Spotify app Client Secret (required for the `spotify` plugin, you can find it on the [dashboard](https://developer.spotify.com/dashboard/)) | `-` |
| `SPOTIFY_REDIRECT_URI` | Spotify app Redirect URI (required for the `spotify` plugin, you can find it on the [dashboard](https://developer.spotify.com/dashboard/)) | `-` |
| `WORLDTIME_DEFAULT_TIMEZONE` | Default timezone to use (required for the `worldtimeapi` plugin, you can get TZ Identifiers from [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)) | `-` |
| `WOLFRAM_APP_ID` | Wolfram Alpha APP ID (required only for the `wolfram` plugin, you can get one [here](https://products.wolframalpha.com/simple-api/documentation)) | - |
| `SPOTIFY_CLIENT_ID` | Spotify app Client ID (required only for the `spotify` plugin, you can find it on the [dashboard](https://developer.spotify.com/dashboard/)) | - |
| `SPOTIFY_CLIENT_SECRET` | Spotify app Client Secret (required only for the `spotify` plugin, you can find it on the [dashboard](https://developer.spotify.com/dashboard/)) | - |
| `SPOTIFY_REDIRECT_URI` | Spotify app Redirect URI (required only for the `spotify` plugin, you can find it on the [dashboard](https://developer.spotify.com/dashboard/)) | - |
| `WORLDTIME_DEFAULT_TIMEZONE` | Default timezone to use, i.e. `Europe/Rome` (required only for the `worldtimeapi` plugin, you can get TZ Identifiers from [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)) | - |
#### Available plugins
| Name | Description | Required API key(s) |
| Name | Description | Required environment variable(s) |
|----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------|
| `weather` | Daily weather and 7-day forecast for any location (powered by [Open-Meteo](https://open-meteo.com)) | `-` |
| `weather` | Daily weather and 7-day forecast for any location (powered by [Open-Meteo](https://open-meteo.com)) | - |
| `wolfram` | WolframAlpha queries (powered by [WolframAlpha](https://www.wolframalpha.com)) | `WOLFRAM_APP_ID` |
| `web_search` | Web search (powered by [DuckDuckGo](https://duckduckgo.com)) | `-` |
| `crypto` | Live cryptocurrencies rate (powered by [CoinCap](https://coincap.io)) - by [@stumpyfr](https://github.com/stumpyfr) | `-` |
| `web_search` | Web search (powered by [DuckDuckGo](https://duckduckgo.com)) | - |
| `crypto` | Live cryptocurrencies rate (powered by [CoinCap](https://coincap.io)) - by [@stumpyfr](https://github.com/stumpyfr) | - |
| `spotify` | Spotify top tracks/artists, currently playing song and content search (powered by [Spotify](https://spotify.com)). Requires one-time authorization. | `SPOTIFY_CLIENT_ID`, `SPOTIFY_CLIENT_SECRET`, `SPOTIFY_REDIRECT_URI` |
| `translate` | Translate text to any language (powered by [DuckDuckGo](https://duckduckgo.com)) | `-` |
| `image_search` | Search image or GIF (powered by [DuckDuckGo](https://duckduckgo.com)) | `-` |
| `worldtimeapi` | Get latest world time (powered by [WorldTimeAPI](https://worldtimeapi.org/)) | `-` |
| `translate` | Translate text to any language (powered by [DuckDuckGo](https://duckduckgo.com)) | - |
| `image_search` | Search image or GIF (powered by [DuckDuckGo](https://duckduckgo.com)) | - |
| `worldtimeapi` | Get latest world time (powered by [WorldTimeAPI](https://worldtimeapi.org/)) | `WORLDTIME_DEFAULT_TIMEZONE` |
Check out the [official API reference](https://platform.openai.com/docs/api-reference/chat) for more details.

View File

@@ -9,10 +9,12 @@ from plugins.web_search import WebSearchPlugin
from plugins.wolfram_alpha import WolframAlphaPlugin
from plugins.worldtimeapi import WorldTimeApiPlugin
class PluginManager:
"""
A class to manage the plugins and call the correct functions
"""
def __init__(self, config):
enabled_plugins = config.get('plugins', [])
plugin_mapping = {

View File

@@ -1,18 +1,20 @@
import os, requests
from typing import Dict
from datetime import datetime
from .plugin import Plugin
class WorldTimeApiPlugin(Plugin):
"""
A plugin to get the current time from a given timezone, using WorldTimeAPI
"""
def __init__(self):
wta_timezone = os.getenv('WORLDTIME_DEFAULT_TIMEZONE')
if not wta_timezone:
default_timezone = os.getenv('WORLDTIME_DEFAULT_TIMEZONE')
if not default_timezone:
raise ValueError('WORLDTIME_DEFAULT_TIMEZONE environment variable must be set to use WorldTimeApiPlugin')
self.defTz = wta_timezone.split('/');
self.default_timezone = default_timezone
def get_source_name(self) -> str:
return "WorldTimeAPI"
@@ -24,37 +26,25 @@ class WorldTimeApiPlugin(Plugin):
"parameters": {
"type": "object",
"properties": {
"area": {
"timezone": {
"type": "string",
"description": f"the continent of timezone identifier. use {self.defTz[0]} if not specified."
},
"location": {
"type": "string",
"description": f"the city/region of timezone identifier. use {self.defTz[1]} if not specified."
"description": f"The timezone identifier (e.g: `Europe/Rome`). Infer this from the location."
f"Use {self.default_timezone} if not specified."
}
},
"required": ["area", "location"],
"required": ["timezone"],
},
}]
async def execute(self, function_name, **kwargs) -> Dict:
areaVal = kwargs.get('area', self.defTz[0])
locVal = kwargs.get('location', self.defTz[1])
url = f'https://worldtimeapi.org/api/timezone/{areaVal}/{locVal}'
timezone = kwargs.get('timezone', self.default_timezone)
url = f'https://worldtimeapi.org/api/timezone/{timezone}'
try:
wtr = requests.get(url).json().get('datetime')
wtr_obj = datetime.strptime(wtr, "%Y-%m-%dT%H:%M:%S.%f%z")
time_24hr = wtr_obj.strftime("%H:%M:%S")
time_12hr = wtr_obj.strftime("%I:%M:%S %p")
res = {
"24hr": time_24hr,
"12hr": time_12hr
}
return {"24hr": time_24hr, "12hr": time_12hr}
except:
res = {"result": "No WorldTimeAPI result was found"}
return res
return {"result": "No result was found"}