diff --git a/README.md b/README.md index bd1a8ab2..b943b791 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ Check out the [Budget Manual](https://github.com/n3d1117/chatgpt-telegram-bot/di | `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)) | `-` | #### Available plugins | Name | Description | Required API key(s) | @@ -120,6 +121,7 @@ Check out the [Budget Manual](https://github.com/n3d1117/chatgpt-telegram-bot/di | `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/)) | `-` | Check out the [official API reference](https://platform.openai.com/docs/api-reference/chat) for more details. diff --git a/bot/plugin_manager.py b/bot/plugin_manager.py index d1e9e71f..ccf6ec6e 100644 --- a/bot/plugin_manager.py +++ b/bot/plugin_manager.py @@ -7,7 +7,7 @@ from plugins.weather import WeatherPlugin from plugins.web_search import WebSearchPlugin from plugins.wolfram_alpha import WolframAlphaPlugin - +from plugins.worldtimeapi import WorldTimeApiPlugin class PluginManager: """ @@ -23,6 +23,7 @@ def __init__(self, config): 'spotify': SpotifyPlugin, 'translate': TranslatePlugin, 'image_search': ImageSearchPlugin, + 'worldtimeapi': WorldTimeApiPlugin, } self.plugins = [plugin_mapping[plugin]() for plugin in enabled_plugins if plugin in plugin_mapping] diff --git a/bot/plugins/worldtimeapi.py b/bot/plugins/worldtimeapi.py new file mode 100644 index 00000000..52f8ced6 --- /dev/null +++ b/bot/plugins/worldtimeapi.py @@ -0,0 +1,60 @@ +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: + raise ValueError('WORLDTIME_DEFAULT_TIMEZONE environment variable must be set to use WorldTimeApiPlugin') + self.defTz = wta_timezone.split('/'); + + def get_source_name(self) -> str: + return "WorldTimeAPI" + + def get_spec(self) -> [Dict]: + return [{ + "name": "worldtimeapi", + "description": f"Get the current time from a given timezone", + "parameters": { + "type": "object", + "properties": { + "area": { + "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." + } + }, + "required": ["area", "location"], + }, + }] + + 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}' + + 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 + } + except: + res = {"result": "No WorldTimeAPI result was found"} + + return res