Skip to content

Commit

Permalink
Merge pull request #360 from noriellecruz/feature/support-functions
Browse files Browse the repository at this point in the history
feat: added plugin to get time
  • Loading branch information
n3d1117 authored Jul 2, 2023
2 parents 5028148 + 7c02110 commit d29d392
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand All @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion bot/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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]

Expand Down
60 changes: 60 additions & 0 deletions bot/plugins/worldtimeapi.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d29d392

Please sign in to comment.