Integrate Lovable with Smooch (Zendesk Sunshine Conversations) using our step-by-step guide to streamline messaging and boost engagement.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
package.json
file in your project’s root directory.
{
"dependencies": {
"smooch-core": "^7.0.3" / Use the latest stable version or adjust according to your needs /
}
}
integrations
(if it does not exist already).integrations
folder, create a new file named smoochIntegration.ts
.smoochIntegration.ts
. This code sets up the Smooch client using your API credentials (which you will provide) and exports a function to send messages through Smooch:
import SmoochCore from 'smooch-core';
const smooch = new SmoochCore({
keyId: process.env.SMOOCHKEYID || 'your-key-id', // Replace with your real Smooch key id
secret: process.env.SMOOCH_SECRET || 'your-secret-key', // Replace with your real Smooch secret
scope: 'app'
});
/**
- Send a text message using Smooch (Zendesk Sunshine Conversations)
- @param appId - Your Smooch App ID
- @param conversationId - The unique conversation id for the user
- @param text - The message content to send
*/
export async function sendMessage(appId: string, conversationId: string, text: string): Promise {
try {
const response = await smooch.appUsers.sendMessage(appId, conversationId, {
type: 'text',
text: text
});
return response;
} catch (error) {
console.error('Failed to send message:', error);
throw error;
}
}
sendMessage
, which you can call to send messages.
app.ts
or similar.sendMessage
function using the following import statement:
import { sendMessage } from './integrations/smoochIntegration';
sendMessage
function when a user registers or performs an action. Insert the snippet below at the appropriate place in your file where you want to trigger a Smooch message:
async function notifyUser() {
const appId = 'your-app-id'; // Replace with your Smooch App ID
const conversationId = 'user-conversation-id'; // Replace with the actual conversation ID
const messageText = 'Hello! Welcome to our service.';
try {
const result = await sendMessage(appId, conversationId, messageText);
console.log('Message sent successfully:', result);
} catch (error) {
console.error('Error while sending message:', error);
}
}
// Call notifyUser() based on your application logic. For example, after a successful user action:
notifyUser();
your-app-id
and user-conversation-id
) with actual values provided by your Smooch setup.
config.ts
in your project’s root and add the following:
export const SMOOCHKEYID = 'your-key-id'; // Replace with your actual key id
export const SMOOCH_SECRET = 'your-secret-key'; // Replace with your actual secret
export const SMOOCHAPPID = 'your-app-id'; // Replace with your Smooch App ID
smoochIntegration.ts
, adjust the code to import these variables from config.ts
instead of directly relying on process.env
:
import SmoochCore from 'smooch-core';
import { SMOOCHKEYID, SMOOCH_SECRET } from '../config';
const smooch = new SmoochCore({
keyId: SMOOCHKEYID,
secret: SMOOCH_SECRET,
scope: 'app'
});
notifyUser
is executed, it should send a message via the Smooch service.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.