How to Cooperate LINE and Cloud BPM

Cooperation with Questetra via LINE APIs

 

Hi, there! I often write about systems collaboration.
* The articles so far are HERE.

Well, I am going to make collaboration with LINE, this time.
Originally, I began to research wondering if I could make a LINE bot in Questetra. However, I gave it up, since I could create one that barely works, because there were problems such inability to do it in Questetra alone, and slow processing speed. (Details will be given later.)

 

Even though I gave up creating the bot, it seems to be useful for notification of completion of processing etc. Since it possible to post a message to LINE by specifying a user or group from Questetra.

* Regarding such notification collaboration to SNS, I have heard some examples of collaboration with “slack“, as another service. I have never heard of a case of collaboration with Chatwork, though it is verified that it is technically possible.

So far, sometimes, I was asked by customers about whether collaboration with LINE was possible.
The strong point of LINE is the wideness of general users, so the education cost is low. Therefore, I am closely watching how Works Mobile, the business version of LINE, will expand in the future.

Thus, I am going to explain how to collaborate with LINE next.


Like the image in the figure below, it only sends an http request through authentication according to the specification of LINE APIs.
* LINE APIs specification is HERE.

QBPMS_to_LINE

1. Create an account to use LINE Messaging API

To obtain an account, apply at “LINE BUSINESS CENTER“, the business oriented portal site of LINE.

You will see two buttons on the screen. Since this is for a research, select “Developer Trial” on the right.

2. Settings of LINE@

After retrieving an account and registering for Message API use, select “LINE@MANAGER” on the “LINE BUSINESS CENTER” account list to set up Messaging API.

First, select “Enable API” at “Settings” &#gt; “Bot Setting”.

bot-setting

Next, set up like so:

  • Use webhooks: Allow
  • Auto Reply Message: Don’t Allow

 
line@-setting

3. Settings of LINE Developer

After finishing settings of LINE@, next, set up the API.

Select “LINE BUSINESS CENTER” on the bottom of the left menu. Then select “LINE Developer” on “Account list”.

Here, set up the “Webhook URL”, first.

Then issue a “Channel Access Token”, and keep it.

api-setting
 


I tried to use “Throwing Message Intermediate Event (HTTP)” for sending an http request. However, it was not possible since it was necessary to set the access token of LINE to the Authorization Bearer of the http header in order to through authentication.
Since “Script Task” is able to deal with it, I used that.
* Please take a look at the “Script task” below.
M230 AUTOMATED STEP Auto Executing Complicated Data Processing (ECMAScript)

The sample script of “Script Task” is as follows. The point is the part of the bearer I mentioned before. This method can be used for collaboration, other than LINE, that also require setting to bearer.

var token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //Access token acquired from the LINE management screen
var json = data.get("0"); //JSON format data to be sent to LINE

var response = httpClient.begin()
  .bearer(token)
  .body(json, "application/json")
  .post("https://api.line.me/v2/bot/message/push");

var code = response.getStatusCode() + "";
var text = response.getResponseAsString() + "";

retVal.put("3", code); //Store processing results
retVal.put("1", text); //Store processing results

The data in JSON format sent to LINE is as follows.

{
    "to": "(Post destination id)",
    "messages":[
        {
            "type":"text",
            "text":"(Message to be Post)"
        }
    ]
}

It was a little troublesome to examine the “id of post destination” above. It is necessary to prepare the ability to receive the Webhook when posting a LINE message and extract it from the content of the http request. (There must be an easier way, please let me know if you know it.) Therefore, you need something like an adapter program to create with Google Apps Script etc. As shown below.


The first thing that caught my attention was that the “Webhook from LINE cannot be received directly with Questetra”.
The format of the http request sent from LINE is limited. The format of the http request that Questetra can receive (receiving in “Message Start Event (HTTP)”) was also limited to some extent, and it could not be adapted to the specification of LINE. So, I solved it by inserting an adapter program to that.
Actually, I made it by processing the received http request from LINE with Google Apps Script (GAS), and throw it to Questetra.
For the adapter program, it doesn’t matter if you use heroku or AWS Lambda + API Gateway. I did it this way only because I am familiar with Google Apps Script.
* Incidentally, please refer to the following for “Message Start Event (HTTP)”.
M221 AUTO START Auto Starting Triggered by HTTP Request

However, what I was not able to solve was that “the reply speed was too slow”.
I came up with the following mechanism / model, but it took about 20 seconds to reply… If it takes such a long time, it is useless as a bot. Thus, I gave up.

LINE_bot
Incidentally, the diagram of the model is shown below (The manual start part is not necessary, since it is for testing).
linebot-model
Sample code for GAS is as shown below. In response to an http request from LINE, necessary data such as replyToken etc. Are extracted and sent to Questetra.

var SHEET_ID = "xxxxxxxxxxxxxxxxxxxx"; //Enter Google Spreadsheet id
var SHEET_NAME_LOG = "logs"; //Prepare "logs" sheet in advance

var QBPMS_URL = "https://xxxxx.questetra.net/System/Event/MessageStart/start"; //According to the  Questetra platform
var QBPMS_PROCESS_MODEL_INFO_ID = "999"; //According to the Process model of Questetra
var QBPMS_NODE_NUMBER = "0"; //According to the Process model of Questetra
var QBPMS_KEY = "xxxxxxxxxx"; //According to the  Questetra platform

function doGet(e) {
  return  receive_(e);
}

function doPost(e) {
  return  receive_(e);
}

/**
* Processing the received http request
*/
function receive_(e) {
  var sheet_log = SpreadsheetApp.openById(SHEET_ID).getSheetByName(SHEET_NAME_LOG);

  //Log output of http request reception
  sheet_log.appendRow([ logDate(), "receive" ]);
  sheet_log.appendRow([ logDate(), "JSON.stringify(e):\n" + JSON.stringify(e) ]);
  var receiveJson = JSON.parse(e.postData.contents);
  var replyToken = receiveJson.events[0].replyToken;
  var message = receiveJson.events[0].message.text;
  sheet_log.appendRow([ logDate(), "replyToken:\n" + replyToken ]);
  sheet_log.appendRow([ logDate(), "message:\n" + message ]);

  var url = QBPMS_URL;
  var payload = 'processModelInfoId=' + QBPMS_PROCESS_MODEL_INFO_ID
    + '&nodeNumber=' + QBPMS_NODE_NUMBER
    + '&key=' + QBPMS_KEY
    + '&q_reply_token=' + replyToken
    + '&q_message=' + message;
  sheet_log.appendRow([ logDate(), "payload:\n" + payload ]);

  var options = {
    "method" : 'post',
    "payload" : payload
  };

  var response = UrlFetchApp.fetch(url, options);
  sheet_log.appendRow([ logDate(), "response:\n" + response.getContentText() ]);

  return HtmlService.createHtmlOutput();
}

/**
* Date and time string creation for log output
*/
function logDate() {
  return Utilities.formatDate(new Date(), "JST", "yyyy-MM-dd HH:mm:ss");
}

The json data generation script sample of “Script Task” is as follows. Since this bot has made only for parroting, so I put the received message as it is as a post message.

var replyToken = data.get("0") + ""; //Reply Token
var message = data.get("1") + ""; //essage to be posted

var jsonObj = {
  "replyToken":"xxx",
  "messages":[]
};

var jsonObj2 = {
  "type":"text",
  "text":"xxx"
};

jsonObj2.text = message;
jsonObj.replyToken = replyToken;
jsonObj.messages[0] = jsonObj2;

var jsonText = JSON.stringify(jsonObj);

retVal.put("3", jsonText);

Sample of http transmission script of “Script Task” is as the below.

var token = data.get("2"); //Access token acquired from the LINE management screen
var json = data.get("3"); //JSON format data to be sent to LINE

var response = httpClient.begin()
  .bearer(token)
  .body(json, "application/json")
  .post("https://api.line.me/v2/bot/message/reply");

var code = response.getStatusCode() + "";
var response = response.getResponseAsString() + "";

retVal.put("4", code); //Store processing results
retVal.put("5", response); //Store processing results


In that way, I diverted a little, but in the method explained here, for example, “For a seminar application from a customer, a notification is sent through LINE” is possible.
Since it depends on the design of the Process Model in Questetra what to use as a notification trigger, it is also possible to “give a notification through LINE when an order occurs”, “notify through LINE when trouble correspondence is completed”.

As another collaboration case, although you need an adapter program to receive the above Webhook, you can also achieve “Starting a process if there is a post containing a specific keyword in LINE”.
A concrete example could be, “when the post containing the keyword “running low on name cards” is detected, a Business card ordering process starts.

 

About Kusaka Tsuyoshi

営業をやってますが、もともとエンジニアなので、プログラミングもやります。
View all posts by Kusaka Tsuyoshi

Recommendations
Prev article - 50. Questetra Tips Automate Processing by Accessing its Own API
Next article - 50. Questetra Tips Can Backlog and Cloud BPM Cooperate?
Another article - Kusaka Tsuyoshi Handle Script Step Well! : Solution for Error (1)

Archive

 RSS