APIキーの使用 - サーバーレス関数

関数のAPIキー

CRM内のサーバーレス関数は、webhookを使用して任意の外部サービスアプリケーションまたはCRM内から呼び出すことができます。ただし通常、(数種類を除く)ほとんどのwebhookは、OAuth2をサポートしていません。その場合、APIキー認証方式を使用して関数を実行できます。この方式は、自分または他のユーザーが任意の場所から関数を呼び出すために使用できます。

APIキーとOAuth2の違いは、認証方法にあります。APIキーは、ヘッダーではなく、リクエストURLで認証される必要があります。

関連項目

サンプル関数:

利用例:

ヘルプデスクとサポート関連ソフトウェアは、顧客とクライアントとの対話を伴うビジネスにおいて不可欠です。ヘルプデスクソフトウェアをCRMと連携させると、あらゆるビジネス情報を一元管理するための時間と労力を大幅に削減できます。

Zendeskは、このようなソフトウェアの1つです。Zohoが開発したZoho Deskもその1つですが、違いはZoho Desk連携はZoho CRMで自動的に行われるのに対し、Zendeskはウィジェットを通して連携させる必要があるという点です。さらに、問い合わせ情報をヘルプデスク間で転送する必要がある場合は、かなり時間がかかります。そのため、Zoho CRMのサーバーレス関数を介してZendeskとZoho Deskを連携させると、非常に便利です。

想定する利用例は似たものになります。「Zendesk」で「問い合わせ」が作成されるたびに、Zoho Deskでも同じ情報を使用して問い合わせを作成します。

必要なアプリケーション:

  • Zendesk
  • Zoho Desk

パート1:接続の設定

関数で外部サービスアプリケーションを利用してデータ転送をしやすくするには、まずZoho CRMと外部サービスソフトウェアとの接続を設定する必要があります。この場合は、Zendeskです。詳細はこちらをご覧ください

Zoho Deskへの接続を作成してください。

パート2:関数の作成

Zoho CRMで関数を作成してください(接続のコードを含む)。

パート2.1:関数の記述

関数コードは次のとおりです。

+

string createTicket(string requestBody){
  if(isNull(requestBody))
  {
    return "No Content";
  }
  if(!requestBody.contains("arguments"))
  {
    return "Invalid Format";
  }
  requestBody = requestBody.get("arguments");
  requestBody = requestBody.get("requestBody");
  requestBody = requestBody.get("details");
  deskURL = "https://desk.zoho.com/api/v1/";
  header = {"orgId":"664950682"};
  departmentId = "264631000000006907"; //Get ticket and user details from requestBody
  userDetails = requestBody.get("user");
  ticketDetails = requestBody.get("ticket"); //Create map
  param = Map(); //Insert Details
  param.put("departmentId",departmentId);
  if(!userDetails.contains("email"))
  {
    return "Email is Mandatory";
  }
  email = userDetails.get("email");
  param.put("email",email);
  contactName = "Guest";
  if(userDetails.contains("full_name"))
  {
    contactName = userDetails.get("full_name");
  }
  else if(userDetails.contains("first_name") && userDetails.contains("last_name"))
  {
    contactName = userDetails.get("first_name") + " " + userDetails.contains("last_name");
  }
  else if(userDetails.contains("first_name"))
  {
    contactName = userDetails.get("first_name");
  }
  else if(userDetails.contains("last_name"))
  {
    contactName = userDetails.get("last_name");
  }
  //Get Contact ID from Contact name
  //Get all Contacts
  contactList = invokeurl
  [
    url :deskURL + "contacts"
    type :GET
    headers:header
    connection:"zoho_desk9"
  ];
  isExistingCustomer = false;
  contactId = "";
  contactList = contactList.get("data");
  for each contactInfo in contactList
  {
    contactEmail = contactInfo.get("email");
    if(!isNull(contactEmail))
    {
      if(email.equals(contactEmail))
      {
        isExistingCustomer = true;
        contactId = contactInfo.get("contactId");
        break;
      }
    }
  }
  info isExistingCustomer;
  if(!isExistingCustomer)
  {
    //Create a new Contact
    contactParam = Map();
    contactParam.put("firstName",userDetails.get("first_name"));
    contactParam.put("lastName",userDetails.get("last_name"));
    contactParam.put("mobile",userDetails.get("mobile"));
    contactParam.put("email",userDetails.get("email"));
    contactParam.put("accountId","264631000000081178");
    contactList = invokeurl
    [
      url :deskURL + "contacts"
      type :POST
      parameters:contactParam.toString()
      headers:header
      connection:"zoho_desk9"
    ];
    contactId = contactList.get("id");
  }
  param.put("contactId",contactId);
  if(!ticketDetails.contains("title"))
  {
    return "title is mandatory";
  }
  param.put("subject",ticketDetails.get("title"));
  if(!ticketDetails.contains("status"))
  {
    return "status is mandatory";
  }
  param.put("status",ticketDetails.get("status"));
  if(ticketDetails.contains("priority"))
  {
    param.put("priority",ticketDetails.get("priority"));
  }
  if(userDetails.contains("organization"))
  {
    organization = userDetails.get("organization");
    if(organization.contains("name"))
    {
      param.put("accountName",organization.get("name"));
    }
  }
  if(userDetails.contains("mobile"))
  {
    param.put("phone",userDetails.get("mobile"));
  }
  if(userDetails.contains("description"))
  {
    param.put("description",userDetails.get("description"));
  }
  info param;
  response = invokeurl
  [
    url :deskURL + "tickets"
    type :POST
    parameters:param.toString()
    headers:header
    connection:"zoho_desk9"
  ];
  if(response.contains("errorMessage"))
  {
    return "Sorry Something went wrong.error ::: " + response.get("errorMessage");
  }
  //Send Notification to Suppot Team
  sendmail
  [
    from :zoho.adminuserid
    to :"deborah.g@zohocorp.com"
    subject :"no-reply"
    message :"'Dear Team,<expression></expression><div><br></div><div>A New Ticket is created inside Zoho DESK through Zendesk.</div><div><br></div><div>Ticket Details - </div><div><br></div><div>User Name - ' + contactName + '</div><div><br></div><div><br></div><div>Request Body = " + requestBody + "</div><div><br></div>'"
  ]
  return response;
}

 

パート2.2:関数をAPIキーを通じて使用可能にするには:

  1. APIの作成が必要な、対応する関数の[設定]アイコンをクリックしてください。
  2. [REST API]をクリックしてください。
  3. [APIキー]スライダーを有効にします。
  4. [保存]をクリックしてください。

パート3:ZendeskでWebhookを作成する。

  1. [管理者] > [拡張機能] > [HTTPターゲット]に移動してください。
  2. 次の情報を入力してください。
    • 関数URL
    • メソッド - GETまたはPOST
    • [コンテンツタイプ]には[JSON]を選択してください。詳細については、ここをクリックしてください。

  • 関数URLは、ステップ2から取得したAPIキーURLです。

パート4:Zendeskでのトリガーの作成:

  1. 条件 - 問い合わせが作成されたとき。
  2. ターゲットの通知 - HTTPターゲット名

JSONの引数のボディー:

{"arguments":
{
"requestBody":{

"details":{
"ticket":
{
"title":"{{ticket.title}}",
"description":"{{ticket.description}}",
"source":"{{ticket.via}}",
"priority":"{{ticket.priority}}",
"due_date":"{{ticket.due_date}}",
"URI":"{{ticket.url}}",
"status":"{{ticket.status}}"
},
"assigne":
{
"email":"{{ticket.assignee.email}}",
"name":"{{ticket.assignee.name}}",
"first_name":"{{ticket.assignee.first_name}}",
"last_name":"{{ticket.assignee.last_name}}"
},
"user":
{
"full_name":"{{current_user.name}}",
"first_name":"{{current_user.first_name}}",
"language":"{{current_user.language}}",
"details":"{{current_user.details}}",
"mobile":"{{current_user.phone}}",
"email":"{{current_user.email}}",
"organization":
{
"details":"{{current_user.organization.details}}",
"name":"{{current_user.organization.name}}"
}}
}}
}}

パート5:リクエストボディー全体を含む引数の設定

webhookが関数にデータを送信する場合、関数内に存在する引数の数を知る方法はありません。この問題に対応するため、関数の1つの引数内にリクエストボディー全体を含めることができます。

関数内でDesk APIを呼び出し、関数を保存します。

結果:

Zendeskでの問い合わせの作成:

問い合わせがZoho Deskで作成されます。