how to check if user id or conversation id or user alreadt exists in sunshine via node.js

512 views Asked by At

I am auto replying a predefined welcome message to user when they are posting a message via fb page or WhatsApp. I want if user already exists in sunshine then not to reply to that user else reply them with welcome message. How can I check if user already exists? 'use strict';

    // Imports
    `enter code here`const express = require('express');
    const bodyParser = require('body-parser');
    const SunshineConversationsApi = require('sunshine-conversations-client');
    var request = require('request')
    // Config
    let defaultClient = SunshineConversationsApi.ApiClient.instance;
    let basicAuth = defaultClient.authentications['basicAuth'];
    basicAuth.username = 'app_XXXXXXX';
    basicAuth.password = 'XXXXXXXXXXXXXXXXX-bSwG85aM8qldJzTt4rgZlf_XQukWKE8ADMno85g';
    const PORT = 5050;

    const apiInstance = new SunshineConversationsApi.MessagesApi()

    // Server https://expressjs.com/en/guide/routing.html
    const app = express();

    app.use(bodyParser.json());

    // Expose /messages endpoint to capture webhooks 
    https://docs.smooch.io/rest/#operation/eventWebhooks
    app.post('/messages', function(req, res) {
    console.log('webhook PAYLOAD:\n', 
     JSON.stringify(req.body.events[0].payload.conversation.id, null, 4));
     const appId = req.body.app.id;
     const trigger = req.body.events[0].type;

    // Call REST API to send message https://docs.smooch.io/rest/#operation/postMessage
    if (trigger === 'conversation:message') {
    const authorType = req.body.events[0].payload.message.author.type;
    const displayName=req.body.events[0].payload.message.author.displayName;
    if(authorType === 'user'){
    const conversationId = req.body.events[0].payload.conversation.id;
    console.log(conversationId);
    console.log(displayName);
    check_conversation_ID_with_sunshine(appId,conversationId);
    //sendMessage(appId, conversationId);
    res.end();
    }
    }
    });

    // Listen on port
    app.listen(PORT, () => {
    console.log(`App listening on port ${PORT}`);
    });

    async function sendMessage(appId, conversationId){
    let messagePost = new SunshineConversationsApi.MessagePost();  
    messagePost.setAuthor({type: 'business'});
    messagePost.setContent({type: 'text', text: 'Thanks boy'});
    let response = await apiInstance.postMessage(appId, conversationId, messagePost);
    //console.log('API RESPONSE:\n', response);
    }
1

There are 1 answers

1
Adam Smooch On

So [a simple] overall process could be:

  • User message comes in (including userId and conversationId)
  • fetch user's conversation
  • if len(response['messages']) == 1 (messages in the conversation)
    • send welcome message
  • (else no-op)

The caveat is that you could get false-negatives, if users send multiple messages, in quick succession, right at the beginning (instead of just one).

Some safe ways to do it would be to either:

  • look for the welcome message at the start of the convo (second message?)
    • i.e. iterate over [the first few] response['messages'], inspecting each one's text/metadata
  • when sending the welcome message, add a metadata flag on the conversation (e.g.: metadata.['welcomeSent']: True), searching for this flag for returning users

Doc references

Get a conversation's messages