1

当用户通过 fb 页面或 WhatsApp 发布消息时,我会自动向用户回复预定义的欢迎消息。我希望如果用户已经存在于阳光中,那么不要回复该用户,否则用欢迎消息回复他们。如何检查用户是否已经存在?'使用严格';

    // 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);
    }
4

1 回答 1

0

所以[一个简单的]整体过程可能是:

  • 用户消息进来(包括userId和conversationId)
  • 获取用户的对话
  • if len(response['messages']) == 1(对话中的消息)
    • 发送欢迎信息
  • (否则无操作)

需要注意的是,如果用户在开始时(而不仅仅是一条)快速连续地发送多条消息,您可能会得到误报。

一些安全的方法是:

  • 在 convo 开始时查找欢迎消息(第二条消息?)
    • 即遍历 [the first few] response['messages'],检查每个人的文本/元数据
  • 发送欢迎消息时,在对话上添加元数据标志(例如metadata.['welcomeSent']: True:),为返回用户搜索此标志

文档参考

获取对话的消息

于 2021-10-04T19:09:19.777 回答