freshworks / fresh-samples

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

GasFreshdesk OO API Class example #22

Open huan opened 8 years ago

huan commented 8 years ago

for issue #21

OO Freshdesk API Class Example.

huan commented 8 years ago

get rid of clear secret key from source code. get key from ScriptProperties.

prasadmunna commented 8 years ago

Can you please add ticket reply with attachment for fresh desk PHP API sample code.

huan commented 8 years ago

@prasadmunna I upgraded my GasFreshdesk library to use the lastest Freshdesk v2 API now, which could use Ticket.reply():

ticket.reply({
  body: 'Hi tom, Still Angry'
  , cc_emails: 'you@example.com' 
})

if you could switch from php to javascript, you can try this lib. sorry for I'm not planing write php code, because I only use javascript under google apps script env now.

You could have a look here: GasFreshdesk

prasadmunna commented 8 years ago

Hi Zixia, Thanks for your reply, but i am using php library for reply on ticket like this $ticket_id =68; $payload = array( 'helpdesk_note[body]' => $postData['message'], 'helpdesk_note[private]' => 'false', 'helpdesk_note[user_id]' => $postData['requester_id'] ); $url = "$fd_domain/helpdesk/tickets/$ticket_id/conversations/note.json"; // $url = "$fd_domain//helpdesk/tickets/$ticket_id/conversations/note.json"; $ch = curl_init ($url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt($ch, CURLOPT_USERPWD, "$token:X"); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $server_output = curl_exec ($ch);

huan commented 8 years ago

I'm loving with this in google apps script:

var MyFreshdesk = new Freshdesk('https://DOMAIN.freshdesk.com', 'KEY')

var ticket = new MyFreshdesk.Ticket({
  description:'A description'
  , subject: 'A subject'
  , email: 'you@example.com'
})

ticket.assign(9000658396)
ticket.note({
  body: 'Hi tom, Still Angry'
  , private: true
})
ticket.reply({
  body: 'Hi tom, Still Angry'
  , cc_emails: ['you@example.com']
})
ticket.setPriority(2)
ticket.setStatus(2)

ticket.del()
ticket.restore()

Logger.log('ticket #' + ticket.getId() + ' was set!')
big-chris commented 8 years ago

Hi Thanks very much for your great code. It's saved me a lot of time.

I'm trying to extend it slightly to set custom ticket fields but its failing with an error and I wonder if you could help? Here's what I've done:

I've added your code to a second file in my project and have added the following:

this.setCustomField = setTicketCustomField

      function setTicketCustomField(customFields) {
        // v1: var retVal = http.put('/helpdesk/tickets/' + getTicketId() + '.json', {
        var retVal = http.put('/api/v2/tickets/' + getTicketId(), {
          custom_fields: customFields
        })

        if (retVal) {
          reloadTicket(getTicketId())
          return this
        }

        throw Error('set custom field fail')          
      } 

I then call this by creating the ticket then calling

var thisCustomFields = '"planned_task_43200":"No"';
  ticket.setCustomField(thisCustomFields);

I then get this error _Error: Freshdesk API v2 failed when calling endpoint[https://xxxxx.freshdesk.com/api/v2/tickets/3651], options[{"muteHttpExceptions":true,"headers":{"Authorization":"Basic d1hmY09jMWdXUEJHT3c1U2JiOlg="},"method":"put","contentType":"application/json","payload":{"custom_fields":"\"planned_task_43200\":\"No\""}}], description[Validation failed] with error: (code[datatype_mismatch], field[custom_fields], message[Should be a key/value pair]) (line 904, file "gas-freshdesk-lib")

Any thoughts?

huan commented 8 years ago

@big-chris glad to hear that gas-freshdesk could help you.

can you fork and commit your changes in github? then there's more easy for me to check the code in details for you.

BTW: you should use custom fields like this, I believe it can help you to fix this bug:

var thisCustomFields = { planned_task_43200: "No" }
ticket.setCustomField(thisCustomFields)
big-chris commented 8 years ago

@zixia thanks a lot for replying.

Sorry I'm pretty new to coding and github so I dont know how to fork and commit changes. I've attached the two files in my project so you can see what I've done.

I tried the suggestion you made about how to use custom fields, but I got a new error message -

Error: Freshdesk API v2 failed when calling endpoint[https://xxxxxx.freshdesk.com/api/v2/tickets/3673], options[{"muteHttpExceptions":true,"headers":{"Authorization":"Basic d1hmY09jMWdXUEJHT3c1U2JiOlg="},"method":"put","contentType":"application/json","payload":{"custom_fields":{"planned_task_43200":"No"}}}], description[Validation failed] with error: (code[invalid_field], field[planned_task_43200], message[Unexpected/invalid field in request]) (line 904, file "gas-freshdesk-lib")

code.gs.txt gas-freshdesk-lib.gs.txt

huan commented 8 years ago

@big-chris so there's lots of details of your freshdesk settings.

I recommand you to contact the support of freshdesk: https://support.freshdesk.com/

I created some tickets over there, and they replied quite fast.

philnatusch commented 6 years ago

@big-chris - probably a bit late for you now as you will have found a way forward, but if you change: custom_field: customFields to: custom_fields: customFields . . . . it should will work.

@zixia - thanks for this code - saved me so much time!