Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

fix(intercom): send valid message creation payloads

Rudy Celeklia2781e890b37
diff --git a/tests/clis/intercom-messages.test.cjs b/tests/clis/intercom-messages.test.cjs
new file mode 100644
index 0000000..03af31e
--- /dev/null
+++ b/tests/clis/intercom-messages.test.cjs
@@ -0,0 +1,30 @@
+const { test } = require('node:test')
+const assert = require('node:assert/strict')
+const { spawnSync } = require('node:child_process')
+const path = require('node:path')
+const cli = path.resolve(__dirname, '../../tools/clis/intercom.js')
+function run(args, oracle = "throw new Error('unexpected fetch')", response = "new Response(JSON.stringify({accepted:true}))") {
+ const code = `global.fetch = async (url, options) => { const assert = require('node:assert/strict'); const parsed = new URL(url); const body = options.body ? JSON.parse(options.body) : undefined; ${oracle}; return ${response}; }; process.argv = ['node',${JSON.stringify(cli)},...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
+ const r = spawnSync(process.execPath,['-e',code],{encoding:'utf8',timeout:10000,env:{...process.env,INTERCOM_API_KEY:'fixture-token'}})
+ assert.equal(r.status,0,r.stderr);return JSON.parse(r.stdout)
+}
+
+const required = ['--body','hello','--admin-id','123','--to','user1']
+for (const type of [undefined,'inapp','in_app']) {
+ test(`in-app message request uses in_app for ${type || 'default'}`, () => {
+ const args = ['messages','create',...required,...(type ? ['--type',type] : [])]
+ assert.equal(run(args, "assert.equal(parsed.pathname,'/messages'); assert.equal(options.method,'POST'); assert.deepEqual(body,{message_type:'in_app',body:'hello',from:{type:'admin',id:'123'},to:{type:'user',id:'user1'}});").accepted,true)
+ })
+}
+for (const template of [undefined,'personal']) {
+ test(`email includes required subject and ${template || 'plain'} template`, () => {
+ assert.equal(run(['messages','create',...required,'--type','email','--subject','Welcome',...(template ? ['--template',template] : [])], `assert.equal(body.message_type,'email'); assert.equal(body.subject,'Welcome'); assert.equal(body.template,${JSON.stringify(template || 'plain')});`).accepted,true)
+ })
+}
+test('missing email subject never sends', () => assert.match(run(['messages','create',...required,'--type','email']).error,/subject/))
+test('unsupported message type never sends', () => assert.match(run(['messages','create',...required,'--type','sms']).error,/type/))
+test('unsupported email template never sends', () => assert.match(run(['messages','create',...required,'--type','email','--subject','Welcome','--template','custom']).error,/template/))
+test('email preview is complete and stays offline', () => {
+ const p=run(['messages','create',...required,'--type','email','--subject','Welcome','--dry-run'])
+ assert.equal(p.body.subject,'Welcome');assert.equal(p.body.template,'plain');assert.equal(p.headers.Authorization,'***')
+})
diff --git a/tools/clis/intercom.js b/tools/clis/intercom.js
index ba96818..1b6ba01 100755
--- a/tools/clis/intercom.js
+++ b/tools/clis/intercom.js
@@ -196,21 +196,30 @@ async function main() {
case 'messages':
switch (sub) {
case 'create': {
- const messageType = args.type || 'inapp'
+ const messageType = !args.type || args.type === 'inapp' ? 'in_app' : args.type
+ if (!['in_app', 'email'].includes(messageType)) { result = { error: '--type must be in_app (or inapp) or email' }; break }
const body = args.body
const adminId = args['admin-id']
const to = args.to
if (!body || !adminId || !to) { result = { error: '--body, --admin-id, and --to (user ID) required' }; break }
- result = await api('POST', '/messages', {
+ const payload = {
message_type: messageType,
body,
from: { type: 'admin', id: adminId },
to: { type: 'user', id: to },
- })
+ }
+ if (messageType === 'email') {
+ if (!args.subject) { result = { error: '--subject required for email messages' }; break }
+ const template = args.template || 'plain'
+ if (!['plain', 'personal'].includes(template)) { result = { error: '--template must be plain or personal' }; break }
+ payload.subject = args.subject
+ payload.template = template
+ }
+ result = await api('POST', '/messages', payload)
break
}
default:
- result = { error: 'Unknown messages subcommand. Use: create --body <text> --admin-id <id> --to <user_id> [--type inapp|email]' }
+ result = { error: 'Unknown messages subcommand. Use: create --body <text> --admin-id <id> --to <user_id> [--type in_app|email] [--subject <text>] [--template plain|personal]' }
}
break
diff --git a/tools/integrations/intercom.md b/tools/integrations/intercom.md
index 900c07d..a63bee7 100644
--- a/tools/integrations/intercom.md
+++ b/tools/integrations/intercom.md
@@ -290,3 +290,17 @@ POST https://api.intercom.io/events
- lead-generation
- customer-support
- in-app-messaging
+
+## CLI message creation
+
+The message creation API uses `in_app` for in-app requests (its response enum
+uses `inapp`). The CLI defaults to `in_app` and keeps `--type inapp` as an alias.
+Email messages require a subject and a `plain` or `personal` template; the CLI
+defaults the email template to `plain`.
+
+```bash
+node tools/clis/intercom.js messages create --body "Welcome" --admin-id 123 --to user1
+node tools/clis/intercom.js messages create --type email --subject "Welcome" --body "Hello" --admin-id 123 --to user1 --template personal
+```
+
+Contract: [Create a message, API 2.11](https://developers.intercom.com/docs/references/2.11/rest-api/api.intercom.io/messages/createmessage).