DEV Community

Aylelite
Aylelite

Posted on

When Should Your Shopify Chat Bot Hand Off to a Human? A Small Rules Engine

When Should Your Shopify Chat Bot Hand Off to a Human? A Small Rules Engine

A live chat AI app is only as good as its handoff. Here's a small, testable function that decides when to pass a chat to your team, and what to do if nobody is online.

  1. Define the rules javascript const ESCALATE_WORDS = /refund|damaged|broken|wrong item|not received|complaint|cancel/i; const ASKS_HUMAN = /human|agent|person|talk to (someone|team)/i;

function shouldHandOff({ message, failedAnswers, botConfident }) {
if (ASKS_HUMAN.test(message)) return 'customer asked';
if (ESCALATE_WORDS.test(message)) return 'sensitive topic';
if (failedAnswers >= 2) return 'bot failed twice';
if (!botConfident) return 'low confidence';
return null;
}

Returning a reason instead of true means you can log why each chat escalated.

  1. Check business hours javascript function isOnline(now = new Date()) { const ist = new Date(now.toLocaleString('en-US', { timeZone: 'Asia/Kolkata' })); const day = ist.getDay(); // 0 = Sunday const hour = ist.getHours(); return day !== 0 && hour >= 10 && hour < 19; }

Adjust the days and hours to your team's schedule.

  1. Route the chat javascript function route(chat) { const reason = shouldHandOff(chat); if (!reason) return { action: 'bot_reply' };

if (isOnline()) {
return { action: 'transfer', reason, transcript: chat.history };
}
return {
action: 'collect_contact',
reason,
reply: 'Our team is offline right now. Leave your email or phone and we will reply by 11 AM tomorrow.',
};
}

Always send the transcript with the transfer so shoppers don't repeat themselves.

Quick tests
javascript
console.log(shouldHandOff({ message: 'My order arrived damaged', failedAnswers: 0, botConfident: true }));
// 'sensitive topic'
console.log(shouldHandOff({ message: 'What is the delivery time?', failedAnswers: 0, botConfident: true }));
// null
Tips
Log every handoff reason and review the top ones weekly.
Don't promise a reply time your team can't keep.
Keep the "talk to a person" option visible, not only automatic.
If the shopper writes in an angry tone, escalate early.

Top comments (0)