Slack message to servicenow incident

Sharath807
Tera Contributor

Hi, Is it possible to get the messages replied in thread in slack message  to respective incident's additional comments through API integration.

2 REPLIES 2

Riya Verma
Kilo Sage
Kilo Sage

Hi @Sharath807 ,

 

Hope you are doing great.

To achieve this, you would need to utilize the Slack API and ServiceNow's REST API.

Here's a high-level approach to implement this integration:

  1. Set up the Slack API integration: Create a Slack app or bot and configure it to access the necessary channels or conversations where incidents are being discussed. 

  2. Establish a webhook in ServiceNow: Create a webhook in ServiceNow to receive incoming Slack messages and process them accordingly. You can use ServiceNow's REST API to create the webhook endpoint.

  3. Configure event subscriptions: Configure event subscriptions in Slack to capture message events in threads. This will enable your Slack app to receive notifications when new messages are posted in threads.

  4. Handle incoming Slack messages in ServiceNow: When a message event is received from Slack, your ServiceNow webhook should parse the payload and extract the relevant information, such as the thread ID, message content, and associated incident number.

  5. Add the message as an additional comment to the respective incident: Using the incident number obtained from the Slack message, make an API call to ServiceNow's REST API to retrieve the corresponding incident record. Then, add the Slack message content as an additional comment to the incident using the AP

 

# Slack message event handler in ServiceNow
def handle_slack_message(event):
    thread_id = event['thread_id']
    message = event['message']
    incident_number = event['incident_number']

    # Retrieve the incident from ServiceNow using incident_number
    incident_api_url = f"https://<your-instance>.service-now.com/api/now/table/incident/{incident_number}"
    incident = requests.get(incident_api_url, headers={"Content-Type": "application/json", "Authorization": "<your-authentication>"})

    # Add the Slack message as an additional comment to the incident
    comments = incident['result']['comments']
    comments.append({'comment': message})
    incident['result']['comments'] = comments

    # Update the incident record with the new comment
    update_incident_api_url = f"https://<your-instance>.service-now.com/api/now/table/incident/{incident_number}"
    update_incident = requests.patch(update_incident_api_url, headers={"Content-Type": "application/json", "Authorization": "<your-authentication>"}, json=incident['result'])

# Code to handle incoming Slack message events and call the handler function
​

 

Please note that the above code snippet is a simplified example and may require modifications to fit your specific ServiceNow instance and Slack integration setup.

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Sharath807
Tera Contributor

@Riya Verma Hi, thankyou for your response.

           Could you please elaborate more on 'step 3' . I tried to   Configure event subscriptions , but not sure what to give in the URL field. I have tried with the instance url as well as incident url , both gave me the error -"Your URL didn't respond with the value of the challenge parameter." Please help .