freshworks / fresh-samples

Samples of code created by freshdesk
184 stars 181 forks source link

Python - Create ticket with attachment #78

Closed mgrandys-r7 closed 1 year ago

mgrandys-r7 commented 1 year ago

I have a problem with creating a FreshDesk ticket with attachments. I've used a code from this repository but it's not working for me.

My setup:

My python code:

# Removed credentials from the script
api_key = "..."
domain = "..."
password = "x"
url = "https://" + domain + ".freshdesk.com/api/v2/"

def create_ticket_with_attachment():
    multipart_data = [
        ('email', ('', 'example@example.com')),
        ('subject', ('', 'Ticket Title')),
        ('status', ('', '2')),
        ('priority', ('', '2')),
        ('cc_emails[]', ('', 'sample_email@domain.com')),
        ('cc_emails[]', ('', 'user_email@domain.com')),
        ('attachments[]', ('logo.png', open("/Users/mgrandys/Desktop/test.png", "rb"), 'image/png')),
        ('description', ('', 'Ticket description.'))
    ]

    response = requests.post(f"{url}tickets", auth=(api_key, password), files=multipart_data)
    response_json = response.json()
    print(response_json)

an the response is:

{
  'description': 'Validation failed',
  'errors': [
    {
      'field': 'subject',
      'message': 'It should be a/an String',
      'code': 'missing_field'
    },
    {
      'field': 'description',
      'message': 'It should be a/an String',
      'code': 'missing_field'
    },
    {
      'field': 'status',
      'message': "It should be one of these values: '2,3,4,5,8,6,7'",
      'code': 'missing_field'
    },
    {
      'field': 'priority',
      'message': "It should be one of these values: '1,2,3,4'",
      'code': 'missing_field'
    },
    {
      'field': 'requester_id',
      'message': 'Please fill at least 1 of requester_id, phone, email, twitter_id, facebook_id, unique_external_id fields',
      'code': 'missing_field'
    }
  ]
}

response headers:

{
  'Date': 'Thu, 13 Oct 2022 08:22:28 GMT',
  'Content-Type': 'application/json; charset=utf-8',
  'Transfer-Encoding': 'chunked',
  'Connection': 'keep-alive',
  'Status': '400 Bad Request',
  'Pragma': 'no-cache',
  'X-Request-Id': '64e9d83d-5d4b-45f3-8736-7aba25201e92',
  'X-Freshdesk-Api-Version': 'latest=v2; requested=v2',
  'X-Rack-Cache': 'invalidate, pass',
  'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate',
  'X-Xss-Protection': '1; mode=block',
  'X-Ua-Compatible': 'IE=Edge,chrome=1',
  'X-Content-Type-Options': 'nosniff',
  'Expires': 'Wed, 13 Oct 2010 00:00:00 UTC',
  'Set-Cookie': '_x_w=7_1; path=/; HttpOnly; secure, _x_m=x_c; path=/; HttpOnly; secure',
  'X-Fw-Ratelimiting-Managed': 'true',
  'X-Ratelimit-Total': '50',
  'X-Ratelimit-Remaining': '48',
  'X-Ratelimit-Used-Currentrequest': '1',
  'X-Envoy-Upstream-Service-Time': '54',
  'X-Trace-Id': '00-8630f03a5747c4ffa61c69ab8a5302a6-ab8370909c78fbc2-00',
  'Nel': '{ "report_to": "nel-endpoint", "max_age": 300, "include_subdomains": true}',
  'Report-To': '{ "group": "nel-endpoint", "max_age": 300, "include_subdomains": true, "endpoints": [{"url": "https://edge-admin.freshedge.net/nelreports"}]}',
  'Server': 'fwe'
}

Also I tried to use ('subject', ('Ticket Title')), instead of ('subject', ('', 'Ticket Title')), but getting another error:

{
  'field': 'subject',
  'message': 'Value set is of type valid file format.It should be a/an String',
  'code': 'datatype_mismatch'
}

response headers:

{
  'Date': 'Thu, 13 Oct 2022 08:19:29 GMT',
  'Content-Type': 'application/json; charset=utf-8',
  'Transfer-Encoding': 'chunked',
  'Connection': 'keep-alive',
  'Status': '400 Bad Request',
  'Pragma': 'no-cache',
  'X-Request-Id': '82a4e008-6f82-496f-8c57-511729049a91',
  'X-Freshdesk-Api-Version': 'latest=v2; requested=v2',
  'X-Rack-Cache': 'invalidate, pass',
  'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate',
  'X-Xss-Protection': '1; mode=block',
  'X-Ua-Compatible': 'IE=Edge,chrome=1',
  'X-Content-Type-Options': 'nosniff',
  'Expires': 'Wed, 13 Oct 2010 00:00:00 UTC',
  'Set-Cookie': '_x_w=7_1; path=/; HttpOnly; secure, _x_m=x_c; path=/; HttpOnly; secure',
  'X-Fw-Ratelimiting-Managed': 'true',
  'X-Ratelimit-Total': '50',
  'X-Ratelimit-Remaining': '49',
  'X-Ratelimit-Used-Currentrequest': '1',
  'X-Envoy-Upstream-Service-Time': '57',
  'X-Trace-Id': '00-b74605956453fe5651157120e73059d5-342bb7f3e4b89944-00',
  'Nel': '{ "report_to": "nel-endpoint", "max_age": 300, "include_subdomains": true}',
  'Report-To': '{ "group": "nel-endpoint", "max_age": 300, "include_subdomains": true, "endpoints": [{"url": "https://edge-admin.freshedge.net/nelreports"}]}',
  'Server': 'fwe'
}

Could you provide a working example of Python3 code for creating tickets with attachments? Thank you in advance :)

mgrandys-r7 commented 1 year ago

I've just solved the problem by replacing the empty string ''params in the tuple to None before: ('subject', ('', 'Ticket Title')), after: ('subject', (None, 'Ticket Title')),