Back to Arky

Support

Run a fixed-step chat flow on the website, take support email on the store's own Resend domain, and answer both from one inbox.

Customers reach a store two ways, and Admin shows both in one inbox:

  • the chat on the store’s website or app, which starts a support flow through its chat entry: a fixed set of messages, buttons, questions and steps that hand the conversation to the team;
  • email to a store address whose domain receives in the store’s own Resend account, which starts the flow of that address’s email entry; the team’s replies go out from the store’s own address.

Every reply comes from a person or from a flow’s fixed steps. Each conversation is a chat, an email thread or both: a chat runs in the language it starts in, an email thread in the entry’s reply language, and a thread staff start in its contact’s language.

1. Write A Flow

A flow is a set of steps with the entries that start it. Each step names the step that comes next, and every chat text has one entry per language the chat runs in. Email the flow or the team sends uses a complete selection, starting from the store’s support_message starter template:

import type { Email } from "arky-sdk";

const { creation_templates } = await sdk.store.emails.get({ store_id: storeId });
const supportReply: Email = {
	email_template_id: creation_templates.support_message,
	sending_address_id: support.id,
	from_name: "Northwind support",
	reply_to: { type: "address", email_address_id: support.id },
};

const flow = await sdk.support.flow.create({
	store_id: storeId,
	id: crypto.randomUUID(),
	key: "default",
	entries: [{ type: "chat", start_step_key: "greeting" }],
	steps: {
		greeting: {
			type: "chat_choice",
			text: { en: "Hi. Ask us anything, or leave your email." },
			buttons: [
				{ label: { en: "Ask a question" }, next_step_key: "question" },
				{ label: { en: "Talk to us" }, next_step_key: "email" },
			],
		},
		question: {
			type: "chat_message",
			text: { en: "Type your question below. The team answers here." },
			next_step_key: "team",
		},
		email: {
			type: "collect_email_contact",
			text: { en: "What's your email?" },
			subject: { en: "Your question to Northwind" },
			reply: supportReply,
			next_step_key: "thanks",
		},
		thanks: {
			type: "chat_message",
			text: { en: "Thanks, {{email}}. A team member will reply here or by email." },
			next_step_key: "team",
		},
		team: { type: "hand_to_team" },
	},
});
StepWhat happens
chat_messageshows its text and goes straight on
chat_choicewaits for one of its buttons; typed text is refused
chat_questionwaits for an answer of its input_type, saved under the step’s key
collect_email_contactasks for the customer’s email, so the team can also reply by email with the step’s reply selection
send_emailemails its own complete selection to the conversation’s email contact
hand_to_teamthe team takes over
resolveresolves the conversation

A step has no language of its own: email goes in the conversation’s language when the template has it, and in the template’s default language otherwise. A conversation runs the flow it started with: editing the flow changes only conversations that start afterwards. When a step can’t run, such as a selection whose sending address is gone, the team gets the conversation and sees why.

2. Put The Chat On The Site

The storefront starts a chat in one of the store’s languages and names the flow to start with. A start always names its flow; a flow whose chat entry hands to the team at once makes a chat that waits for the team.

import { initialize } from "arky-sdk/storefront";
import { createOrderToken } from "arky-sdk/utils";

const arky = initialize("arky_pk_...", {
	locale: "en",
	createVisitorIdentity: () => ({
		customer_id: crypto.randomUUID(),
		session_id: crypto.randomUUID(),
	}),
	createSessionId: () => crypto.randomUUID(),
	createId: () => crypto.randomUUID(),
	createOrderToken,
});

const started = await arky.support.conversation.start({
	language: "en",
	flow_key: "default",
});
localStorage.setItem("support-chat", started.conversation.id);

The conversation id comes from your createId. When the answer is lost, starting the same chat again (the same language and flow) reuses that id, so the customer gets one conversation. Pass an id yourself when you want to pick it.

There is no chat token. A chat belongs to the customer of the browser’s session: the SDK starts or resumes that session before the first call, and every later read or message needs only the conversation id. Another browser, or a customer who is signed out of that session, can’t read it.

Show what the conversation waits for:

import type { ConversationStateName, StorefrontConversationMessage } from "arky-sdk";

function nextPrompt(state: ConversationStateName, messages: StorefrontConversationMessage[]) {
	if (state === "resolved") return null;
	if (state === "waiting_for_team") return { input: "text", prompt_message_id: null };
	const latest = messages
		.filter((message) =>
			["flow_choice", "flow_question", "flow_email_contact"].includes(message.type.type),
		)
		.at(-1);
	if (latest?.type.type === "flow_choice") return { buttons: latest.type.buttons, prompt_message_id: latest.id };
	if (latest?.type.type === "flow_question") return { input: latest.type.input_type, prompt_message_id: latest.id };
	if (latest?.type.type === "flow_email_contact") return { input: "email_contact", prompt_message_id: latest.id };
	return null;
}

Send a button press, text or an email contact, and name the prompt it answers:

const shown = nextPrompt(started.conversation.state, started.messages);

const response = await arky.support.conversation.sendMessage({
	conversation_id: started.conversation.id,
	input: { type: "button", label: "Talk to us" },
	prompt_message_id: shown?.prompt_message_id ?? undefined,
});

await arky.support.conversation.sendMessage({
	conversation_id: started.conversation.id,
	input: { type: "email_contact", contact: { email: "[email protected]", language: "en" } },
	prompt_message_id: emailPrompt.id,
});

The message id comes from createId too. When the answer is lost, keep the message on screen and send the same input for the same prompt again: the SDK reuses the kept id, so the message is added once. A different message is a new message with a new id. The SDK keeps only the id and a fingerprint of the message, never its text.

  • An email-contact prompt takes { type: "email_contact", contact: { email, language } }, with one of the store’s languages. From then on the team can also reply by email.
  • If the chat moved on since the prompt was shown, the answer is refused with 409 CONVERSATION.STALE_PROMPT and nothing is added: read the conversation again and show the current prompt.
  • Poll the conversation every few seconds to pick up the team’s replies; findMessages pages from the newest messages back.
  • A resolved chat takes no more messages (409 CONVERSATION.ENDED): offer a button that starts a new one.

The arky.io website’s own chat widget follows this pattern. Its source is the ChatWidget.svelte component of the arky.io repository.

3. Take Support Email

Arky doesn’t log into the store’s mailbox. Support email reaches Arky through the store’s own Resend account: turn on receiving for the store’s domain there, so its MX records point at Resend, and use an address on that domain. Then give a flow an incoming_email entry for the address:

const support = await sdk.notification.emailAddress.create({
	store_id: storeId,
	id: crypto.randomUUID(),
	email: "[email protected]",
	email_provider_id: connection.id,
});

const readiness = await sdk.notification.emailAddress.getReadiness({ store_id: storeId, id: support.id });
if (readiness.receiving.type !== "ready") console.log("Turn on receiving for the domain in Resend first");

const emailFlow = await sdk.support.flow.create({
	store_id: storeId,
	id: crypto.randomUUID(),
	key: "support-email",
	entries: [
		{
			type: "incoming_email",
			email_address_id: support.id,
			reply_language: "en",
			reply: supportReply,
			start_step_key: "acknowledge",
		},
	],
	steps: {
		acknowledge: { type: "send_email", email: supportReply, next_step_key: "team" },
		team: { type: "hand_to_team" },
	},
});
  1. A customer writes to [email protected]; Resend receives it and hands it to Arky through the webhook at Arky’s address in that Resend account.
  2. Arky files the email into its conversation by its reply headers, or starts one with the address’s entry: the sender becomes the customer, the conversation runs in the entry’s reply_language, and the entry’s steps run, here an acknowledgment and the handover to the team. Mail reaches the address whether it is in To, Cc or Bcc. A reply from another address of the sender, such as an alias, starts its own conversation; mail from one of the store’s own addresses is ignored; and automatic mail, such as an out-of-office reply, joins its thread without reopening it or alerting the team.
  3. The team replies in Admin; Arky sends the reply as Northwind support <[email protected]> with the conversation’s reply selection, through the store’s own Resend account.
  4. The customer’s answer comes back to [email protected] and joins the same conversation.

Each receiving address belongs to one entry. The acknowledgment answers a new conversation’s first email at most once a day per address, the same limit a chat’s flow email has, and never answers automatic email, such as out-of-office replies or mailing lists. An email Arky can’t file, such as mail to an address no entry names, waits in the store’s Resend inbox (sdk.notification.resendEvent.find) until staff apply it again or ignore it.

4. Answer From The Inbox

const inbox = await sdk.support.conversation.find({
	store_id: storeId,
	statuses: ["waiting_for_team"],
	limit: 25,
});

const conversation = inbox.items[0];

await sdk.support.conversation.reply({
	store_id: storeId,
	conversation_id: conversation.id,
	id: crypto.randomUUID(),
	expected_updated_at: conversation.updated_at,
	text: "Your order leaves tomorrow morning.",
	delivery: conversation.type.type === "chat" ? "chat" : "email",
	resolve: true,
});

A reply is delivered by chat, email or chat_and_email; resolve: true sends and resolves in one step. A customer’s new email on a resolved conversation reopens it. An email reply goes out with the conversation’s reply selection, whose template prints the team’s message; staff change it with selectReplyEmail.

5. Start An Email Thread

Staff can write to a customer first, for example about an order. The thread starts with the team and its first message is the staff reply; the customer’s answer joins it like any reply:

await sdk.support.conversation.startEmail({
	store_id: storeId,
	id: crypto.randomUUID(),
	message_id: crypto.randomUUID(),
	customer_id: customerId,
	contact: { email: "[email protected]", language: "en" },
	subject: "About your order 1042",
	reply: supportReply,
	text: "Your parcel is delayed by a day. Sorry about that.",
});

6. Show A Received Email In The Chat

A received email stays out of a customer’s chat. When a conversation has both a chat and an email thread, staff can show one received email in the chat, or hide it again, with the message version they read:

await sdk.support.conversation.changeChatVisibility({
	store_id: storeId,
	conversation_id: conversation.id,
	message_id: receivedMessage.id,
	expected_updated_at: receivedMessage.updated_at,
	visible_in_chat: true,
});
Note

See the Support API reference for every record and rule.