Home>
I want to achieve

This questionIn the answer to

・ Response based on conversation history
When a webhook sends a message, it searches for the latest conversation history by the source type and the corresponding Id. From the previous conversation (including the response from the bot side), identify that it is a continuation message and branch.
As a flow,
Incoming call
→ Search for conversations that have not timed out from id
→ No
→ Initial support
→ Yes
→ Identify the pattern of conversation continuation
→ Pattern A response
→ Pattern B response
...

There was a thing called. I want to do something like this.

Code currently in use

I will describe it for the time being.
(If possible, I want to store the reply text in botMessage.)

var TOKEN = // access token;
var URL = "https://api.line.me/v2/bot/message/reply";
function doPost (event) {
    var request = JSON.parse (event.postData.contents);
    var replyToken = request.events [0] .replyToken;
    var botMessage;// Determine the response text here
    // Omitted, the content of botMessage is decided here
var payload = JSON.stringify ({{
        "replyToken": replyToken,
        "messages": [{
            "type": "text",
            "text": botMessage
        }]
    });
    UrlFetchApp.fetch (URL, {
        "headers": {
            "Content-Type": "application/json;charset = UTF-8",
            "Authorization": "Bearer" + TOKEN
        },
        "method": "post",
        "payload": payload
    });
    return;
}
Supplement

Use Google Drive.
I'm a complete beginner myself, so I would appreciate it if you could give me an example.

  • Answer # 1

    const doPost = (e) =>{
        const request = JSON.parse (e.postData.contents);
        const type = request.events [0] .source.type;
        if (type! =='user') return;// Ignore if not user. (Because it is troublesome to think about follow)
        const contentsType = request.events [0] .message.type;
        if (contentsType! =='text') return;// Ignore if not text message
        const userId = request.events [0] .source.userId;
        const message = request.events [0] .message.text;
        const botMessage = handleBook (userId, message);
        // please send.
    }
    const fetchRow = (array, pos, needle) =>{
        const found = array.filter (e =>e [pos] === needle);
        return found.length>0? Found [0]: found;
    }
    const isTimedOut = (target) =>{
        const timeout = 5;
        const now = new Date ();
        const delta = now --target/(1000 * 60);
        return delta>timeout;
    }
    const findStatusForUser = (dat, uid) =>{
        const row = fetchRow (dat, 0, uid);
        if (row.length<0) {
            return undefined;
        }
        if (isTimedOut (row [1])) {
            return undefined;
        }
        return row.slice (2);
    }
    const updateStatusForUser = (sheet, dat, uid, arr) =>{
        const row = fetchRow (dat, 0, uid);
        if (row.length<0) {
            sheet.appendRow ([uid, new Date (), ... arr]);
            return;
        }
        const value = dat.map (e =>e [0] === uid? [uid, new Date (), ... arr]: e);
        sheet.getRange (1,1, value.length, value [0] .length) .setValues ​​(value);
    }
    const getAnswer = (message, e) =>{// Please inflate the conversation appropriately in the future
        if (e === undefined) {
            return ["What's wrong?", "waitfor1", "none"];
        }
        if (message! == "weather") {
            return [What is `${message}? I don't know`, "waitfor1", "none"]
        }
        if (e [0] === "waitfor2") {
            return [`${message} weather is sunny`, "complete", "none"];
        }
        return ["Where is the weather?", "Waitfor2", "none"];
    }
    const handleBook = (uid, message) =>{
        const sheetName = "Sheet 1";
        const sheet = SpreadsheetApp.getActive (). getSheetByName (sheetName);
        const dat = sheet.getDataRange (). getValues ​​();
        const previous = findStatusForUser (dat, uid);
        const ans = getAnswer (message, previous);
        updateStatusForUser (sheet, dat, uid, ans.slice (1));
        return ans [0];
    }

  • Answer # 2

    I solved it by another method (spreadsheet).