From f30c66fb1b98bfb674d1b7f21a9b8fa4189dcb11 Mon Sep 17 00:00:00 2001 From: Jacob Weiss Date: Tue, 16 Apr 2024 09:50:16 -0600 Subject: [PATCH] add zendesk tool --- cookbook/tools/zendesk_tools.py | 18 ++++++++++++ phi/tools/zendesk.py | 50 +++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 cookbook/tools/zendesk_tools.py create mode 100644 phi/tools/zendesk.py diff --git a/cookbook/tools/zendesk_tools.py b/cookbook/tools/zendesk_tools.py new file mode 100644 index 000000000..2be7fa67e --- /dev/null +++ b/cookbook/tools/zendesk_tools.py @@ -0,0 +1,18 @@ +from phi.assistant import Assistant +from phi.tools.zendesk import ZendeskTools +import os + +# Retrieve Zendesk credentials from environment variables +zd_username = os.getenv("ZENDESK_USERNAME") +zd_password = os.getenv("ZENDESK_PW") +zd_company_name = os.getenv("ZENDESK_COMPANY_NAME") + +if not zd_username or not zd_password or not zd_company_name: + raise EnvironmentError("Please set the following environment variables: ZENDESK_USERNAME, ZENDESK_PW, ZENDESK_COMPANY_NAME") + +# Initialize the ZendeskTools with the credentials +zendesk_tools = ZendeskTools(username=zd_username, password=zd_password, company_name=zd_company_name) + +# Create an instance of Assistant and pass the initialized tool +assistant = Assistant(tools=[zendesk_tools], show_tool_calls=True) +assistant.print_response("How do I login?", markdown=True) diff --git a/phi/tools/zendesk.py b/phi/tools/zendesk.py new file mode 100644 index 000000000..782b8e62e --- /dev/null +++ b/phi/tools/zendesk.py @@ -0,0 +1,50 @@ +from phi.tools import Toolkit +import json +import re +import requests + +class ZendeskTools(Toolkit): + """ + A toolkit class for interacting with the Zendesk API to search articles. + It requires authentication details and the company name to configure the API access. + """ + + def __init__(self, username: str, password: str, company_name: str): + """ + Initializes the ZendeskTools class with necessary authentication details + and registers the search_zendesk method. + + Parameters: + username (str): The username for Zendesk API authentication. + password (str): The password for Zendesk API authentication. + company_name (str): The company name to form the base URL for API requests. + """ + super().__init__(name="zendesk_tools") + self.username = username + self.password = password + self.company_name = company_name + self.register(self.search_zendesk) + + def search_zendesk(self, search_string: str) -> str: + """ + Searches for articles in Zendesk Help Center that match the given search string. + + Parameters: + search_string (str): The search query to look for in Zendesk articles. + + Returns: + str: A JSON-formatted string containing the list of articles without HTML tags. + + Raises: + ConnectionError: If the API request fails due to connection-related issues. + """ + auth = (self.username, self.password) + url = f"https://{self.company_name}.zendesk.com/api/v2/help_center/articles/search.json?query={search_string}" + try: + response = requests.get(url, auth=auth) + response.raise_for_status() + clean = re.compile('<.*?>') + articles = [re.sub(clean, '', article["body"]) for article in response.json()['results']] + return json.dumps(articles) + except requests.RequestException as e: + raise ConnectionError(f"API request failed: {e}") \ No newline at end of file