LINE is a popular SNS tool in the Asian region, especially in Japan.
The number of users in Japan is about 76 million. The daily active user (DAU) boasts a very high figure of 85%, both number of users and frequency of use.
It is an API that realizes two-way communication with users through LINE's account.
It is possible to develop interactive Bot application using LINE talk screen.
This API has already been used in many corporate LINE accounts, such as courier delivery redelivery acceptance and new year greeting creation.
It is a program execution environment of serverless architecture previously announced by SAP.
You can run the program created with Node.js.
var request = require("request");
var https = require("https");
module.exports = {
handler: function(event, context) {
if (event.data) {
if (event.data.events[0].type == "message") {
if (event.data.events[0].message.type == "image") {
var imageid = event.data.events[0].message.id;
// get image from below link
//https://api.line.me/v2/bot/message/{imageid}/content
var options = {
method: 'GET',
uri: 'https://api.line.me/v2/bot/message/' + imageid + '/content',
encoding: null,
auth: {
bearer: "*********************" // LINE BOT Access Token
}
};
request(options, function(error, response, body) {
// get binary image
var binaryimage = new Buffer(body);
// request to SAP ML Server
var boundary = createBoundary();
let optionstosap = {
host: "sandbox.api.sap.com",
port: 443,
path: "/ml/imageclassification/classification",
method: "POST",
headers: {
"APIKey": "YOUR_API_KEY",
"Content-Type": "multipart/form-data; boundary=" + boundary
}
};
var reqtosap = https.request(optionstosap, function(res) {
var data2 = '';
res.setEncoding("utf8");
res.on("data", (chunk) => {
data2 += chunk;
var resultarr = JSON.parse(data2).predictions[0].results;
var columns = [];
for (var k = 0; k < resultarr.length; k++) {
var column = {};
column.title = resultarr[k].label.substring(0, 35);
column.text = Math.round(resultarr[k].score * 100 * 10) / 10 + "%";
var actions = [];
var action = {};
action.type = "uri";
action.label = "Google翻訳";
action.uri = "https://translate.google.co.jp/?hl=ja#en/ja/" + encodeURIComponent(resultarr[k].label);
actions.push(action);
column.actions = actions;
columns.push(column);
}
var options2 = {
method: 'POST',
uri: 'https://api.line.me/v2/bot/message/reply',
body: {
replyToken: event.data.events[0].replyToken,
messages: [{
type: "template",
altText: "Image classification result.",
template: {
type: "carousel",
columns: columns
}
}]
},
auth: {
bearer: "*********************" // LINE BOT Access Token
},
json: true
};
request(options2, function(err, res, body) {
console.log(JSON.stringify(res));
});
});
reqtosap.on('end', function() {
console.log('data;', data2);
res.end();
});
});
reqtosap.on("error", function(e) {
console.error(e.message);
});
var buffer = unicode2buffer(
'--' + boundary + '\r\n' + 'Content-Disposition: form-data; name="files"; filename="myimage.png"\r\n' +
'Content-Type: image/png\r\n\r\n'
);
var buffer = appendBuffer(buffer,
binaryimage
);
var buffer = appendBuffer(buffer,
unicode2buffer(
'\r\n' + '--' + boundary + '--'
)
);
reqtosap.write(Buffer.from(buffer));
reqtosap.end();
});
}
}
}
console.log('event data ' + JSON.stringify(event.data));
return 'hello world from a function!';
}
}
function createBoundary() {
var multipartChars = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var length = 30 + Math.floor(Math.random() * 10);
var boundary = "---------------------------";
for (var i = 0; i < length; i++) {
boundary += multipartChars.charAt(Math.floor(Math.random() * multipartChars.length));
}
return boundary;
}
function unicode2buffer(str) {
var n = str.length,
idx = -1,
byteLength = 512,
bytes = new Uint8Array(byteLength),
i, c, _bytes;
for (i = 0; i < n; ++i) {
c = str.charCodeAt(i);
if (c <= 0x7F) {
bytes[++idx] = c;
} else if (c <= 0x7FF) {
bytes[++idx] = 0xC0 | (c >>> 6);
bytes[++idx] = 0x80 | (c & 0x3F);
} else if (c <= 0xFFFF) {
bytes[++idx] = 0xE0 | (c >>> 12);
bytes[++idx] = 0x80 | ((c >>> 6) & 0x3F);
bytes[++idx] = 0x80 | (c & 0x3F);
} else {
bytes[++idx] = 0xF0 | (c >>> 18);
bytes[++idx] = 0x80 | ((c >>> 12) & 0x3F);
bytes[++idx] = 0x80 | ((c >>> 6) & 0x3F);
bytes[++idx] = 0x80 | (c & 0x3F);
}
if (byteLength - idx <= 4) {
_bytes = bytes;
byteLength *= 2;
bytes = new Uint8Array(byteLength);
bytes.set(_bytes);
}
}
idx++;
var result = new Uint8Array(idx);
result.set(bytes.subarray(0, idx), 0);
return result.buffer;
}
function appendBuffer(buf1, buf2) {
var uint8array = new Uint8Array(buf1.byteLength + buf2.byteLength);
uint8array.set(new Uint8Array(buf1), 0);
uint8array.set(new Uint8Array(buf2), buf1.byteLength);
return uint8array.buffer;
}
{
"dependencies": {
"request": "*"
}
}
Trigger | Description |
HTTP | It issues a URL and fires when an HTTP request is made for that URL. |
Timer | Processing can be executed at regular intervals. |
Event | Combined with SAP Enterprise Messaging, you can operate the application with the event sent from outside as a trigger. |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
36 | |
8 | |
7 | |
5 | |
5 | |
4 | |
4 | |
4 | |
3 | |
3 |