GithubHelp home page GithubHelp logo

facetoe / zenpy Goto Github PK

View Code? Open in Web Editor NEW
339.0 20.0 159.0 6.24 MB

Python wrapper for the Zendesk API

License: GNU General Public License v3.0

Python 99.96% Makefile 0.03% Shell 0.01%
python wrapper zendesk-api python-wrapper zendesk client api

zenpy's Introduction

Build Status

Zenpy

Zenpy is a Python wrapper for the Zendesk, Chat and HelpCentre APIs. The goal of the project is to make it easy to write clean, fast, Pythonic code when interacting with Zendesk progmatically. The wrapper tries to keep API calls to a minimum. Wherever it makes sense objects are cached, and attributes of objects that would trigger an API call are evaluated lazily.

Zenpy supports both Python2 and Python3.

Please report bugs!

Quickstart

from zenpy import Zenpy
from zenpy.lib.api_objects import Ticket
# Create a Zenpy instance
zenpy_client = Zenpy(**credentials)

# Create a new ticket
zenpy_client.tickets.create(Ticket(subject="Important", description="Thing"))

# Perform a simple search
for ticket in zenpy_client.search('PC LOAD LETTER', type='ticket', assignee='facetoe'):
    # No need to mess around with ids, linked objects can be accessed directly.
    print(ticket.requester.name)

    # All objects can be converted to a Python dict.
    print(ticket.to_dict())

    # Or to JSON.
    print(ticket.to_json())

Examples

Searching open and pending tickets for a specific user and sort them by descending
zenpy_client.search(type='ticket', status_less_than='closed', assignee='[email protected]', sort_order='desc')
Searching only opened tickets
zenpy_client.search(type='ticket', status='open')
Exporting all tickets matching the query

By default, Search API has a limit of 1000 results in total. Search Export API allows exporting unlimited number of results, so if you'd like to export all results, use this method instead:

for ticket in zenpy_client.search_export(type='ticket', status='open'):
    print(ticket)

Read more about these limitations:

Search results limits

Search Export API release notes

Creating a ticket with a different requester
from zenpy.lib.api_objects import Ticket, User

zenpy_client.tickets.create(
    Ticket(description='Some description',
           requester=User(name='bob', email='[email protected]'))
)
Commenting on a ticket
from zenpy.lib.api_objects import Comment

ticket = zenpy_client.tickets(id=some_ticket_id)
ticket.comment = Comment(body="Important private comment", public=False)
zenpy_client.tickets.update(ticket)
Adding a HTML comment to a ticket
from zenpy.lib.api_objects import Ticket, Comment

zenpy_client.tickets.create(Ticket(
    subject='Html comment example',
    comment=Comment(body='The smoke is very colorful',
                    html_body='<h2>The smoke is <i>very</i> colourful</h2>'))
)
Appending tags to a ticket
from zenpy.lib.api_objects import Ticket

ticket = zenpy_client.tickets(id=some_ticket_id)
ticket.tags.extend(['onetag', 'twotag', 'threetag', 'four'])
zenpy_client.tickets.update(ticket)
Uploading an attachment
from zenpy.lib.api_objects import Comment

# Upload the file (or file-like object) to Zendesk and obtain an Upload instance
upload_instance = zenpy_client.attachments.upload('/tmp/awesome_file.txt')

ticket = zenpy_client.tickets(id=some_ticket_id)
ticket.comment = Comment(body='This comment has my file attached', uploads=[upload_instance.token])
zenpy_client.tickets.update(ticket)
Creating a ticket with a custom field set
from zenpy.lib.api_objects import CustomField, Ticket

ticket_audit = zenpy_client.tickets.create(Ticket(
    subject='Has custom field',
    description="Wow, such field",
    custom_fields=[CustomField(id=43528467, value=1337)]
))
Updating a custom field on a ticket
from zenpy.lib.api_objects import CustomField
ticket = zenpy_client.tickets(id=some_ticket_id)
ticket.custom_fields.append(CustomField(id=43528467, value=1337))
zenpy_client.tickets.update(ticket)
Applying a Macro to a ticket
# Execute the show_macro_effect() method which returns what the macro *would* do.
# The method accepts either Zenpy objects or ids.
macro_result = zenpy_client.tickets.show_macro_effect(ticket_id_or_object, macro_id_or_object)

# Update the ticket to actually change the ticket.
zenpy_client.tickets.update(macro_result.ticket)
Adding a photo to a user
user = zenpy_client.users(id=user_id)
user.remote_photo_url = 'http://domain/example_photo.jpg'
zenpy_client.users.update(user)
List all categories from help center
categories = zenpy_client.help_center.categories()
for category in categories:
    pass
List all help center articles
articles = zenpy_client.help_center.articles(section=section)
for article in articles:
    pass
List all help center articles in a section
section = zenpy_client.help_center.categories.sections(category_id=category.id)
articles = zenpy_client.help_center.sections.articles(section=section)
for article in articles:
    pass
Create new category in help center
from zenpy import Zenpy
from zenpy.lib.api_objects.help_centre_objects import Category
new_category = zenpy_client.help_center.categories.create(
            Category(
                name="Category name",
                description="Category description",
                locale="en-us",
                created_at=datetime.now(),
                updated_at=datetime.now()
            )
        )
print(new_category.to_dict(serialize=True))
Create new section in help center
from zenpy import Zenpy
from zenpy.lib.api_objects.help_centre_objects import Section
new_section = zenpy_client.help_center.sections.create(
            Section(
                name="Section name",
                description="Section description",
                category_id=new_category.id,
                locale="en-us",
                created_at=datetime.now(),
                updated_at=datetime.now()
            )
        )
print(new_section.to_dict(serialize=True))
Create new article in help center
from zenpy import Zenpy
from zenpy.lib.api_objects.help_centre_objects import Article
new_article = zenpy_client.help_center.articles.create(
                    section=new_section.id,
                    article=Article(
                        name="Article Name",
                        body="<p>Article html content body</p>",
                        locale="en-us",
                        title="Article title",
                        section_id=new_section.id,
                        created_at=datetime.now(),
                        updated_at=datetime.now()
                    ),
                )
print(new_article.to_dict(serialize=True))
Working with webhooks
Show a webhook
webhook = zenpy_client.webhooks(id=WEBHOOK_ID) 
List webhooks
# Just list all the webhooks
for webhook in zenpy_client.webhooks.list():
    pass # Do something with it

# Filter the webhooks by a string in the name
for webhook in zenpy_client.webhooks.list(filter='some string'):
    pass # Do something with it

# Using sorting and pagination according to https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/#list-webhooks
zenpy_client.webhooks.list(sort='name')
zenpy_client.webhooks.list(page_before=X, page_size=Y)
zenpy_client.webhooks.list(page_after=N, page_size=Y)
Creating a webhook that uses basic authentication
from zenpy.lib.api_objects import Webhook

new_webhook = Webhook(
    authentication={
        "add_position": "header",
        "data": {
            "password": "hello_123",
            "username": "john_smith"
        },
        "type": "basic_auth"
    },
    endpoint="https://example.com/status/200",
    http_method="GET",
    name="Example Webhook",
    description="Webhook description",
    request_format="json",
    status="active",
    subscriptions=["conditional_ticket_events"],
) 
zenpy_client.webhooks.create(new_webhook)
Creating a webhook that uses no authentication
new_webhook = Webhook(
    endpoint="https://example.com/status/200",
    http_method="GET",
    name="Example Webhook",
    description="Webhook description",
    request_format="json",
    status="active",
    subscriptions=["conditional_ticket_events"],
) 
zenpy_client.webhooks.create(new_webhook)
Creating a webhook that uses bearer token authentication
new_webhook = Webhook(
    authentication={
        "add_position": "header",
        "data": {
            "token": "{{token}}"
        },
        "type": "bearer_token"
    },
    # other fields
) 
zenpy_client.webhooks.create(new_webhook)
Updating a webhook
from zenpy.lib.api_objects import Webhook

webhook = zenpy_client.webhooks(id=WEBHOOK_ID) 

# Note: We need a brand new object because of API specific requirements for 'update'
# https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/#update-webhook

new_webhook = Webhook(
                    name="New name",
                    request_format="json",
                    http_method="GET",
                    endpoint="https://example.com/status/200",
                    status="active",
                    authentication={
                      "add_position": "header",
                      "data": {
                          "password": "hello_123",     # As we can't get it back we need to pass it again from scratch
                          "username": "john_smith"
                      },
                      "type": "basic_auth"
                  },
)
response = zenpy_client.webhooks.update(webhook.id, new_webhook)
Partially updating (patching) a webhook
webhook = zenpy_client.webhooks(id=WEBHOOK_ID)
webhook.name = 'A new name'
response = zenpy_client.webhooks.patch(webhook)
Cloning a webhook
from zenpy.lib.api_objects import Webhook

an_existing_webhook = zenpy_client.webhooks(id=WEBHOOK_ID) 
new_webhook = zenpy_client.webhooks.clone(an_existing_webhook)

# Or just
new_webhook = zenpy_client.webhooks.clone(WEBHOOK_ID)
Working with secrets
secret = zenpy_client.webhooks.show_secret(webhook)
print(secret.secret)

secret = zenpy_client.webhooks.reset_secret(webhook)
print(secret.secret)
Testing webhooks
# Testing an existing webhook "as is""
response = zenpy_client.webhooks.test(webhook)

# Testing an existing webhook with modifications 
response = zenpy_client.webhooks.test(
                    webhook, 
                    request=dict(
                      endpoint='https://example.org/'
                    )
)

# Sending a test request without creating a webhook
response = zenpy_client.webhooks.test(
                    request=dict(
                        endpoint="https://example.org",
                        request_format="json",
                        http_method="GET",
                    )
                )
Getting a webhook invocations

API documentation

wh_filters = {
    'filter[from_ts]': '2023-12-04T12:00:00Z',
    'filter[to_ts]': '2023-12-04T16:00:00Z',
    'filter[status]': 'success',
}

for invocations in zenpy.webhooks.invocations(webhook_id, **wh_filters):
    pass
Pagination

Please refer to the official documentation to get details. Also check this article: Which endpoints are supported?

# An old style offset pagination, not recommended. Since August 15, 2023, is limited to 100 pages.
fields = zenpy_client.ticket_fields()
# Or
fields = zenpy_client.ticket_fields(cursor_pagination=False)

# A new cursor offset pagination
fields = zenpy_client.ticket_fields(cursor_pagination=True) # is equal to 100 results per page
# Or
fields = zenpy_client.ticket_fields(cursor_pagination=50) # 50 results per page

Documentation

Check out the documentation for more info.

Contributions

Contributions are very welcome. I've written an explanation of the core ideas of the wrapper in the Contributors Guide.

zenpy's People

Contributors

ahmedsabriz avatar andrewswait avatar bastianzim avatar blodter avatar cryptomail avatar deybhayden avatar dstarod avatar dym avatar facetoe avatar fmartingr avatar gizmo93 avatar guptaa3 avatar insspb avatar jgillmanjr avatar jwalterclark avatar kapoorabhish avatar makcyd avatar michaelcosby avatar mpata avatar northisup avatar onyb avatar oxmane avatar ptr314 avatar ravichandrasekaran avatar sapphire64 avatar shaakmiller avatar sleepysysadmin avatar thepumpinglemma avatar wontonst avatar ypperlig avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zenpy's Issues

Ticket update returning weird error from api_objects

So I'm trying out zenpy for editing tickets but when I'm trying update it I receive this weird error:

>>> ticket = zend.tickets.get_ticket(38884)
>>> ticket.tags.append(u'wrapper_testing')
>>> zend.tickets.update(ticket)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/xander-lubuntu/.virtualenvs/sabackend/local/lib/python2.7/site-packages/zenpy/lib/api_objects.py", line 25, in __repr__
return super().__str__()
TypeError: super() takes at least 1 argument (0 given)

It updates the ticket yes, but this is something I would not want to receive or see.
Can anyone shine a light on this thing?

Mock over zenpy

Hi,

It's any way to make a mock to the tickets library?. For example, make a mock to zenpy.tickets.create function?

I make a mock, but the test not working, the call_count give me 0 value (i try with zenpy.lib.api.TicketApi.create, zenpy.lib.api.CRUDApi.create, with the same result).

Thanks

Is it time to cut a new release?

Hi facetoe,

Thank you for implementing my request with issue #36. I am hoping to use the latest changes in production very soon. Is the current state of master ready for version 1.0.3? ๐Ÿ˜„

User's related information

Hi guys,

Is there a way to get user's related information based on his name or his id like:

zenpy.search("john doe", type='user', related_information=True)

?

I would like to get users infos and his number of requested tickets or opened tickets.

Incremental Ticket Metrics Event support

Hi,
First of all let me say, great module! Really helped me out. I did though find the incremental ticket metrics missing.

I opened a branch and added it myself, but I do not have permissions to publish the branch and open a pull request.

I would love for this to be added (through my PR or not), as I don't want to maintain a fork for ZenPy only for this.

Thanks,
Uri

zenpy hangs

Hi There

I have a problem where regularly my zenpy client just hangs without throwing an exception...

Here my code....

ZD_EXCEPTIONS = (
    ZenpyException,
    APIException,
    RecordNotFoundException,
    ZenpyCacheException
    )

try:
    usr = []
    for obj in zenpy.group_memberships(timeout=0.05):
        if obj.user.role == 'agent' and \
          obj.user.to_dict()['user_fields']['custom_name'] is not None:
            data = [
                obj.user.to_dict()['user_fields']['custom_name'].replace(
                    '.domain.com', '').replace('-', '_'),
                obj.user.id,
                obj.user.email,
                obj.user.organization.id,
                obj.user.organization.name,
                ]
            if data not in usr:
                usr.append(data)
except ZD_EXCEPTIONS as error:
    print('Problem connecting to Zendesk API. {}'.format(error))
    sys.exit(1)

I assume I am hitting rate limit or something but it is not caught by the timeout either. I found this article regarding best practices on rate limit. https://support.zendesk.com/hc/en-us/articles/203691336-Best-practices-for-avoiding-rate-limiting

Any suggestions what I can do?

Many Thanks
Christoph

"objects" directory was there? and now it's gone?

I installed zenpy from source about 2 weeks ago.
it included "/objects/" subdirectory.

Today I can't find the "objects" subdir (neither in pipy nor in github).
Where did it go?
Am I missing something?

Issue creating a new 'User' object

Hi,

First and foremost, thanks for this zendesk api wrapper.

After reading through the zenpy documentation though, I am having trouble creating new users.

After logging in with my credentials:

zenpy = zenpy.Zenpy(**creds)

I can make zenpy queries on zenpy.users (etc.), but when I try to create a new user:

user = User(name='Jim', email='[email protected]')

I get the following error message:

Traceback (most recent call last):
  ...
    user = User(name='Jim', email='[email protected]')
NameError: name 'User' is not defined

This was the example given in the documentation.

I am hesitant because I'm afraid it is an issue on my end (am I missing something?), but would like to see if anyone else has this issue since I've exhausted a lot of different options.

Thanks,
Preston

Users endpoint not sideloading organizations, etc.

Hey!
The users endpoint does not seem to side load organizations.
https://github.com/facetoe/zenpy/blob/master/zenpy/lib/endpoint.py#L243

The http request does show the query parmater
https://test.zendesk.com/api/v2/users/1.json&include=organizations,abilities,roles,identities,groups

but I am unable to access organizations on the result, for example:

(Pdb) zendesk_user = zendesk.users(id=4727211738)
(Pdb) zendesk_user.organizations
*** AttributeError: 'User' object has no attribute 'organizations'

Sort Example

Can you please update the readme to provide an example on how to query or search with a sort_by parameter?

query on users

Hello,

How can I get a user thanks to his email ?
zenpy.users(email="theemailadress") doesnt work. I got a resultgenerator instead of an object.

Regards

HW

SearchEndpoint improper query string generation

It seems that the if-elif conditions defined in SearchEndpoint don't properly handle cases when the value is a datetime object OR the value is of list type and key == ids. In order for those arguments to make it into the final query string, they can't be handled as elif conditions. They should probably be their own if conditions so that the code can fall through to assessment of the keys, and then modify renamed_kwargs.

create_or_update_many returns 403

I think this is a bug in Zendesk as even their example returns a 403:

curl -v -u [email protected]/token:sometoken https://facetoe.zendesk.com/api/v2/users/create_or_update_many.json   -H "Content-Type: application/json" -X POST -d '{"users": [{"name": "Roger Wilco", "email": "[email protected]", "role": "agent"}, {"external_id": "account_54321", "name": "Woger Rilco", "email": "[email protected]", "role": "admin"}]}'
...

> POST /api/v2/users/create_or_update_many.json HTTP/1.1
> Host: facetoe.zendesk.com
> User-Agent: curl/7.47.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 185
> 
* upload completely sent off: 185 out of 185 bytes
< HTTP/1.1 403 Forbidden

Add a comment to a ticket

I'm trying to facilitate the work of our agents by adding a private comment to each incoming ticket, where we specify a few additional details about the requester, specific to our service. However, I can't figure out if it's possible to create a comment, either public or private, for a given ticket. Is this implemented at all? If not, any chance it will be?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.