Skip to content

Instantly share code, notes, and snippets.

@dugjason
Created April 6, 2021 21:59
Show Gist options
  • Save dugjason/09696b58067db65eeb05a5070a62e998 to your computer and use it in GitHub Desktop.
Save dugjason/09696b58067db65eeb05a5070a62e998 to your computer and use it in GitHub Desktop.
Front API: Applying tags to custom conversations
## Overview
This example shows us how to create a conversation in Front using the Receive custom messages
endpoint (https://dev.frontapp.com/reference/messages-1#post_channels-channel-id-incoming-messages),
then apply a tag to that newly created conversation based on the message_uid attribute returned
from that API call.
## Implementation
1. Create the inbound message, using the "Receive Custom Messages" endpoint - https://dev.frontapp.com/reference/messages-1#post_channels-channel-id-incoming-messages
2. Grab the `message_uid` from that response
3. Use Resource aliases ( https://dev.frontapp.com/reference/introduction#resource-aliases ) with the `GET /messages` endpoint to fetch the message by UID
4. In the response, the message will have a conversation API URI we can use to apply the tag
5. Use the `POST /conversations/:conv_id/tags` endpoint to add a tag to this conversation - https://dev.frontapp.com/reference/conversations-1#post_conversations-conversation-id-tags
const fetch = require('node-fetch')
const { FRONT_API_KEY } = process.env
const FRONT_API_HOST = 'https://api2.frontapp.com'
// Replace these IDs with real values from your own Front account
const CHANNEL_ID = 'cha_123'
const TAG_IDS = ['tag_123']
const getRequestHeaders = {
'Authorization': `Bearer ${FRONT_API_KEY}`,
}
const postRequestHeaders = Object.assign(getRequestHeaders, {
'Content-Type': 'application/json'
})
async function run() {
// First, create the message
const message = await fetch(
`${FRONT_API_HOST}/channels/${CHANNEL_ID}/incoming_messages`, {
method: 'POST',
body: JSON.stringify({
sender: { handle: 'example_sender_handle' },
body: 'Message contenta',
}),
headers: postRequestHeaders,
})
// Wait for the response
const messageData = await message.json()
// It can sometimes take a moment for the message to be created after this point.
// Wait a moment before attempting to fetch the message data
await sleep(500)
// Now using the messageData.message_uid, we can look up the conversation
const fullMessage = await fetch(
`${FRONT_API_HOST}/messages/alt:uid:${messageData.message_uid}`, {
method: 'GET',
headers: getRequestHeaders,
}
)
// Wait for the response
const fullMessageData = await fullMessage.json()
// Extract the conversation resource URI from the message payload
const conversationURI = fullMessageData._links.related.conversation
const applyTag = await fetch(
`${conversationURI}/tags`, {
method: 'POST',
body: JSON.stringify({
tag_ids: TAG_IDS,
}),
headers: postRequestHeaders,
}
)
}
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment