0

我尝试使用谷歌应用程序脚本通过freshdesk API创建带有附件的票证。

Freshdesk 在这里有谷歌应用程序脚本示例代码:https ://github.com/freshdesk/fresh-samples/tree/master/google_script ,但没有附件演示。

我尝试了以下代码,但 /helpdesk/tickets.json api 似乎总是返回错误。

如何使用 UrlFetchApp 在谷歌应用脚​​本中发布附件?

function createFreshdeskTicketWithAttachments() {

 var API_KEY = PropertiesService.getScriptProperties().getProperty('FreshDeskApiKey')
  if (!API_KEY) throw new Error('FreshDeskApiKey not found in script properties.')

  var ENDPOINT = Utilities.formatString('https://%s.freshdesk.com', 'zixia')

  var headers = {
    'Authorization': 'Basic ' + Utilities.base64Encode(API_KEY + ':X')
    , 'Content-type': 'application/json'
    //'Content-type': 'multipart/form-data'
  };

  var response = UrlFetchApp.fetch("https://circleci.com/gh/AKAMobi/ucapp/tree/master.svg?style=svg")
  var fileBlob = response.getBlob()
  Logger.log("%s:%s"
             , response.getResponseCode()
             , fileBlob.getContentType()
            )

  var payload = {
    helpdesk_ticket: {
      description: 'TEST 4'
      , subject: "TEST 4"
      , email: "test@test.com"
      , priority: 1
      , status: 2
      , attachments: { '': [ { resource: fileBlob } ] }
    }
  } 

  //Adds the extensions that are needed to post a new ticket to the end of the url
  var url = ENDPOINT + '/helpdesk/tickets.json';

  var options = {
    'method': 'post',
    'headers': headers,
    'payload': JSON.stringify(payload),
//    'payload': payload,
    muteHttpExceptions: true
  };

  var response = UrlFetchApp.fetch(url, options);

  Logger.log('resp: %s, %s'
             , response.getResponseCode()
             , response.getContentText()
            )

}
4

2 回答 2

0

使用 nc 调试后:

nc -l 211.99.222.55 3333

我发现有效载荷数组应该这样写:

  var payload = {
    'helpdesk_ticket[description]': 'test jjjj'
    , 'helpdesk_ticket[subject]': 'test jj'
    , 'helpdesk_ticket[email]': 'test@test.com'
    , 'helpdesk_ticket[attachments][][resource]': fileBlob1
    , 'helpdesk_ticket[attachments][][resource]': fileBlob2
  }

但 fileBlob2 不会发布到服务器,因为数组的键是重复的。我试图将 'helpdesk_ticket[attachments][][resource]' 更改为 'helpdesk_ticket[attachments][1][resource]' 但它不起作用。

所以这是一个可行的版本,它只支持单个附件:

function createTicket() {

  var API_KEY = PropertiesService.getScriptProperties().getProperty('FreshDeskApiKey')
  if (!API_KEY) throw new Error('FreshDeskApiKey not found in script properties.')

  var ENDPOINT = Utilities.formatString('https://%s.freshdesk.com', 'zixia')

  var headers = {
    'Authorization': 'Basic ' + Utilities.base64Encode(API_KEY + ':X')
  }

    var response = UrlFetchApp.fetch("https://circleci.com/gh/AKAMobi/ucapp/tree/master.svg?style=svg")
  var fileBlob1 = response.getBlob()

  var fileBlob2 = UrlFetchApp
  .fetch("http://imgcache.qq.com/open_proj/proj_qcloud_v2/qcloud_2015/css/img/global/internet-plus.png")
  .getBlob()

  var payload = {
    'helpdesk_ticket[description]': 'test jjjj'
    , 'helpdesk_ticket[subject]': 'test jj'
    , 'helpdesk_ticket[email]': 'test@test.com'
    , 'helpdesk_ticket[attachments][][resource]': fileBlob1
    , 'helpdesk_ticket[attachments][][resource]': fileBlob2
  }
//
//  for (var i=0; i<attachments.length; i++) {
//    payload['helpdesk_ticket[attachments][][resource]'] = attachments[i]
//  }
//  
//  for (var k in payload) {
//    Logger.log('%s = %s', k, payload[k])
//  }

  var options = {
    'method': 'post'
    , 'headers': headers
    , 'payload': payload
    , muteHttpExceptions: true
  }

  var url = ENDPOINT + '/helpdesk/tickets.json'
//  url = 'http://211.99.222.55:3333'
  var response = UrlFetchApp.fetch(url, options)

  Logger.log('resp: %s, %s'
             , response.getResponseCode()
             , response.getContentText()
            )    

}
于 2015-10-30T06:59:14.877 回答
0

2016 年 1 月 20 日更新

我为gas中的freshdesk v2 api编写了一个名为GasFreshdesk的库,非常易于使用(支持附件):

var MyFreshdesk = new GasFreshdesk('https://domain.freshdesk.com', 'TOKEN')

var ticket = new MyFreshdesk.Ticket({
  description:'A description'
  , subject: 'A subject'
  , email: 'you@example.com'
  , attachments: [ 
    Utilities.newBlob('TEST DATA').setName('test-data.dat')
    , Utilities.newBlob('TEST DATA2').setName('test-data2.dat')
  ]
})

ticket.assign(9000658396)
ticket.note({
  body: 'Hi tom, Still Angry'
  , private: true
  , attachments: [ 
    Utilities.newBlob('TEST DATA').setName('test-data.dat')
    , Utilities.newBlob('TEST DATA2').setName('test-data2.dat')
  ]
})
ticket.reply({
  body: 'Hi tom, Still Angry'
  , cc_emails: ['you@example.com']
  , attachments: [ 
    Utilities.newBlob('TEST DATA').setName('test-data.dat')
    , Utilities.newBlob('TEST DATA2').setName('test-data2.dat')
  ]
})
ticket.setPriority(2)
ticket.setStatus(2)

ticket.del()
ticket.restore()

Logger.log('ticket #' + ticket.getId() + ' was set!')

GasFreshdesk Github:https ://github.com/zixia/gas-freshdesk

旧帖子:

最后,谷歌应用程序脚本可以通过 UrlFetchApp.fetch 将多个附件附加到freshdesk 上的新票证,并由我自己手动制作“多部分”参数生成器辅助功能。

  1. 将附件放在另一个数组有效负载中:

    var payload = [ ['helpdesk_ticket[description]', 'test jjjj'] , ['helpdesk_ticket[subject]', 'test jj'] , ['helpdesk_ticket[email]', 'test@test.com'] , [ 'helpdesk_ticket[attachments][][resource]',fileBlob2],['helpdesk_ticket[attachments][][resource]',fileBlob1]

  2. 使用 makeMultipartBody 函数生成 requestBody:

    var multipartBody = makeMultipartBody(有效负载)

完整代码在这里:

function createTicket2() {

  var API_KEY = PropertiesService.getScriptProperties().getProperty('FreshDeskApiKey')
  if (!API_KEY) throw new Error('FreshDeskApiKey not found in script properties.')

  var ENDPOINT = Utilities.formatString('https://%s.freshdesk.com', 'zixia')

  var headers = {
    'Authorization': 'Basic ' + Utilities.base64Encode(API_KEY + ':X')
  }

    var response = UrlFetchApp.fetch("https://circleci.com/gh/AKAMobi/ucapp/tree/master.svg?style=svg")
  var fileBlob1 = response.getBlob()

  var fileBlob2 = UrlFetchApp
  .fetch("http://imgcache.qq.com/open_proj/proj_qcloud_v2/qcloud_2015/css/img/global/internet-plus.png")
  .getBlob()

  var payload = [
    ['helpdesk_ticket[description]', 'test jjjj']
    , ['helpdesk_ticket[subject]', 'test jj']
    , ['helpdesk_ticket[email]', 'test@test.com']
    , ['helpdesk_ticket[attachments][][resource]', fileBlob2]
    , ['helpdesk_ticket[attachments][][resource]', fileBlob1]
  ]

  var boundary = '-----CUTHEREelH7faHNSXWNi72OTh08zH29D28Zhr3Rif3oupOaDrj'

  payload = makeMultipartBody(payload, boundary)

  //Logger.log('payload: %s', payload)
  //return

  var options = {
    'method': 'post'
    , contentType: "multipart/form-data; boundary=" + boundary
    , 'headers': headers
    , 'payload': payload
    , muteHttpExceptions: true
  }

  var url = ENDPOINT + '/helpdesk/tickets.json'
//  url = 'http://211.99.222.55:3333'
  var response = UrlFetchApp.fetch(url, options)

  Logger.log('resp: %s, %s'
             , response.getResponseCode()
             , response.getContentText()
            )    
}


function makeMultipartBody(payload, boundary) {

  var body = Utilities.newBlob('').getBytes()
  Logger.log(payload)

  for (var i in payload) {
    var [k, v] = payload[i]

    Logger.log('############ %s = %s', k, v)

    if (v.toString() == 'Blob') {

      // attachment
      body = body.concat(
        Utilities.newBlob(
          '--' + boundary + '\r\n'
          + 'Content-Disposition: form-data; name="' + k + '"; filename="' + v.getName() + '"\r\n'
        + 'Content-Type: ' + v.getContentType() + '\r\n\r\n'
      ).getBytes())

      body = body
      .concat(v.getBytes())
      .concat(Utilities.newBlob('\r\n').getBytes())

    } else {

      // string
      body = body.concat(
        Utilities.newBlob(
          '--'+boundary+'\r\n'
          + 'Content-Disposition: form-data; name="' + k + '"\r\n\r\n'
          + v + '\r\n'
        ).getBytes()
      )

    }

  }

  body = body.concat(Utilities.newBlob("\r\n--" + boundary + "--\r\n").getBytes())

  return body

}
于 2015-10-30T08:57:23.927 回答