Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion groovehq/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .client import Groove

__all__ = ('Groove',)
__all__ = ("Groove",)
228 changes: 204 additions & 24 deletions groovehq/client.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
__all__ = ('Groove',)
__all__ = ("Groove",)

import datetime
import re

import requests

class Groove(object):

class Groove(object):
def __init__(self, api_token):
self._api_token = api_token
self._session = requests.Session()
self._session.headers = self._headers()

def _headers(self):
return {
'Authorization': 'Bearer {}'.format(self._api_token),
}
return {"Authorization": "Bearer {}".format(self._api_token)}

def list_mailboxes(self):
"""
Return all mailboxes.
"""
resp = self._session.get("https://api.groovehq.com/v1/mailboxes")
return resp.json()["mailboxes"]

def list_folders(self, **kwargs):
"""
Return all folders.

:param mailbox: The ID or email address of a mailbox to filter by
"""
params = kwargs.items()
resp = self._session.get(
"https://api.groovehq.com/v1/folders", params=params
)
return resp.json()["folders"]

def list_agents(self):
"""
Return all agents.
"""
resp = self._session.get("https://api.groovehq.com/v1/agents")
return resp.json()["agents"]

def get_agent(self, email):
"""
Find the agent whose email is *email*.

:param email: the agent's email.
"""
resp = self._session.get(f"https://api.groovehq.com/v1/agents/{email}")
return resp.json()["agent"]

def list_tickets(self, **kwargs):
"""
Expand All @@ -31,28 +65,85 @@ def list_tickets(self, **kwargs):
:param folder: the ID of a folder
"""

params = { k:unicode(v) for k, v in kwargs.items() }
resp = self._session.get('https://api.groovehq.com/v1/tickets',
params=params)
return resp.json()['tickets']
params = kwargs.items()
resp = self._session.get(
"https://api.groovehq.com/v1/tickets", params=params
)
return resp.json()["tickets"]

def get_messages(self, ticket_number, **kwargs):
"""
Get all messages for a particular ticket.

See https://www.groovehq.com/docs/messages#listing-all-messages for more
details.
See https://www.groovehq.com/docs/messages#listing-all-messages for
more details.

:param ticket_number: the integer ticket number
:param page: the page number
:param per_page: how many results to return per page, defaults to 25
"""
params = { k:unicode(v) for k, v in kwargs.items() }
params = kwargs.items()

url = ('https://api.groovehq.com/v1/tickets/{}/messages'
.format(ticket_number))
url = "https://api.groovehq.com/v1/tickets/{}/messages".format(
ticket_number
)
resp = self._session.get(url, params=params)
return resp.json()['messages']
return resp.json()["messages"]

def create_ticket(
self,
body,
from_,
to,
mailbox="",
assigned_group="",
assignee="",
sent_at=None,
note=False,
send_copy_to_customer=False,
state="unread",
subject="",
tag="",
):

if sent_at is None:
sent_at = datetime.datetime.now()

data = {
"body": body,
"from": from_,
"to": to,
"mailbox": mailbox,
"assigned_group": assigned_group,
"assignee": assignee,
"sent_at": sent_at.strftime("%a, %d %b %Y %H:%M:%S"),
"note": note,
"send_copy_to_customer": send_copy_to_customer,
"state": state,
"subject": subject,
"tag": tag,
}
resp = self._session.post(
"https://api.groovehq.com/v1/tickets/", json=data
)
return resp.json()["ticket"]

def update_ticket(self, ticket_number, state):
"""Update a ticket's state.

Args:
ticket_number (int): The ID of the ticket to update.
state (str): The state of the ticket. Can be "unread", "opened",
"pending", "closed", or "spam".

Returns:
None

"""
self._session.put(
f"https://api.groovehq.com/v1/tickets/{ticket_number}/state",
json={"state": state},
)

def create_message(self, ticket_number, author, body, note=True):
"""
Expand All @@ -67,18 +158,107 @@ def create_message(self, ticket_number, author, body, note=True):
:param note: whether this should be a private note, or sent to the
customer.
"""
data = {
'author': author,
'body': body,
'note': note
}
url = ('https://api.groovehq.com/v1/tickets/{}/messages'
.format(ticket_number))
data = {"author": author, "body": body, "note": note}
url = "https://api.groovehq.com/v1/tickets/{}/messages".format(
ticket_number
)
resp = self._session.post(url, json=data)

result = resp.json()
new_url = ret['message']['href']
nums = re.findall(r'\d+', new_url)
new_url = result["message"]["href"]
nums = re.findall(r"\d+", new_url)

if len(nums) > 0:
return nums[-1]

def get_customer(self, customer_id):
"""
Fetch a customer whose id is *customer_id*.

*customer_id* can be an actual ID or an email address.

See https://www.groovehq.com/docs/customers#finding-one-customer
for details.

:param customer_id: the ID / email address of the customer.
"""
url = "https://api.groovehq.com/v1/customers/{}".format(customer_id)
resp = self._session.get(url)
result = resp.json()
return result["customer"]

def update_customer(
self,
email,
name=None,
about=None,
twitter_username=None,
title=None,
company_name=None,
phone_number=None,
location=None,
linkedin_username=None,
custom=None,
):
data = {"email": email}
if name is not None:
data["name"] = name
if about is not None:
data["about"] = about
if twitter_username is not None:
data["twitter_username"] = twitter_username
if title is not None:
data["title"] = title
if company_name is not None:
data["company_name"] = company_name
if phone_number is not None:
data["phone_number"] = phone_number
if location is not None:
data["location"] = location
if linkedin_username is not None:
data["linkedin_username"] = linkedin_username
if custom is not None:
data["custom"] = {}
resp = self._session.put(
f"https://api.groovehq.com/v1/customers/{email}", json=data
)
result = resp.json()
return result["customer"]

def create_customer(
self,
email,
name=None,
about=None,
twitter_username=None,
title=None,
company_name=None,
phone_number=None,
location=None,
linkedin_username=None,
custom=None,
):
data = {"email": email}
if name is not None:
data["name"] = name
if about is not None:
data["about"] = about
if twitter_username is not None:
data["twitter_username"] = twitter_username
if title is not None:
data["title"] = title
if company_name is not None:
data["company_name"] = company_name
if phone_number is not None:
data["phone_number"] = phone_number
if location is not None:
data["location"] = location
if linkedin_username is not None:
data["linkedin_username"] = linkedin_username
if custom is not None:
data["custom"] = {}
resp = self._session.post(
"https://api.groovehq.com/v1/customers/", json=data
)
result = resp.json()
return result["customer"]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
author_email = '[email protected]',
packages = ['groovehq'],
include_package_data = True,
install_requires = ['requests==2.9.1'],
install_requires = ['requests'],
license='LICENSE',
url = 'https://github.com/cardforcoin/groovehq',
keywords = 'groovehq groove api helpdesk',
Expand Down