Chapter 10

Integrating a Help Desk

When building a chatbot for eCommerce, integrating with a helpdesk system is crucial to ensure smooth communication and efficient resolution of customer issues. Every helpdesk platform, from Zendesk to Freshdesk, has its own set of features, strengths, and weaknesses. However, they all share some fundamental capabilities that we can leverage to build robust integrations.

Key functions like retrieving a ticket, fetching the latest customer message, replying to that message, assigning the ticket to a particular agent, handing off to a human agent, or adding internal notes are essential for a well-rounded integration. The goal is to streamline these actions so that the chatbot can effectively manage customer interactions—from classification and response to collaboration with human agents.

Helpdesk platforms typically provide comprehensive API documentation, which can be used to implement these functions precisely. This documentation allows developers to interact with the helpdesk’s backend, perform necessary actions, and integrate them seamlessly into the chatbot’s workflow.

Below is an example of how an integration with Zendesk’s API might look in JavaScript:

const axios = require('axios');

const ZENDESK_API_URL = 'https://your_subdomain.zendesk.com/api/v2';  
const ZENDESK_API_TOKEN = 'your_api_token';  
const ZENDESK_EMAIL = 'your_email@example.com';

// Set up Axios for API calls  
const zendeskApi = axios.create({  
    baseURL: ZENDESK_API_URL,  
    auth: {  
        username: ZENDESK_EMAIL,  
        password: ZENDESK_API_TOKEN  
    },  
    headers: {  
        'Content-Type': 'application/json'  
    }  
});

// Function to get a ticket by ID  
async function getTicket(ticketId) {  
    try {  
        const response = await zendeskApi.get(`/tickets/${ticketId}.json`);  
        return response.data.ticket;  
    } catch (error) {  
        console.error('Error fetching ticket:', error);  
        throw error;  
    }  
}

// Function to get the latest message from a ticket  
async function getLatestMessage(ticketId) {  
    try {  
        const ticket = await getTicket(ticketId);  
        return ticket.latest_comment;  
    } catch (error) {  
        console.error('Error fetching latest message:', error);  
        throw error;  
    }  
}

// Function to reply to a ticket  
async function replyToTicket(ticketId, message) {  
    try {  
        const response = await zendeskApi.post(`/tickets/${ticketId}.json`, {  
            ticket: {  
                comment: {  
                    body: message,  
                    public: true  
                }  
            }  
        });  
        return response.data.ticket;  
    } catch (error) {  
        console.error('Error replying to ticket:', error);  
        throw error;  
    }  
}

// Function to assign the ticket to an agent  
async function assignTicket(ticketId, agentId) {  
    try {  
        const response = await zendeskApi.put(`/tickets/${ticketId}.json`, {  
            ticket: {  
                assignee_id: agentId  
            }  
        });  
        return response.data.ticket;  
    } catch (error) {  
        console.error('Error assigning ticket:', error);  
        throw error;  
    }  
}

// Function to add a note to the ticket  
async function addNoteToTicket(ticketId, note) {  
    try {  
        const response = await zendeskApi.post(`/tickets/${ticketId}.json`, {  
            ticket: {  
                comment: {  
                    body: note,  
                    public: false  
                }  
            }  
        });  
        return response.data.ticket;  
    } catch (error) {  
        console.error('Error adding note to ticket:', error);  
        throw error;  
    }  
}

// Example usage:  
(async () => {  
    const ticketId = 123456; // Example ticket ID  
    const agentId = 78910; // Example agent ID  
    const message = "Here is the update you requested.";  
    const note = "Customer mentioned a possible return request.";

    await replyToTicket(ticketId, message);  
    await assignTicket(ticketId, agentId);  
    await addNoteToTicket(ticketId, note);

    console.log('Actions completed successfully.');  
})();

This code demonstrates how to interact with Zendesk’s API to perform key actions. These actions—getting a ticket, responding to messages, assigning tickets, and adding notes—are fundamental to the chatbot's ability to handle customer interactions effectively.

Later in our discussion on the Company Policy Processor, we'll show how this help desk integration plays a crucial role. For instance, after the chatbot classifies an intent (such as a "Where is my order?" request), it can use this integration to log the interaction, add notes for human agents, and send a response, all while adhering to company policy guidelines.

By understanding and implementing these integrations, we can build a chatbot that not only responds to customer queries but also operates seamlessly within the existing customer service infrastructure, ensuring efficient and accurate resolutions.

← CH09: Integrating AI
CH11: Integrating an eCommerce Platform →