How to receive different types of messages from SUSI Line Bot

In this blog, I will explain how to receive different types of messages responses from LINE Bot. This includes text, sticker, video, audio, location etc types of responses. Follow this tutorial to make SUSI AI LINE Bot. How does LINE Messaging API work? The messaging API of LINE allows data to be passed between the server of SUSI AI and the LINE Platform. When a user sends a message to SUSI bot, a webhook is triggered and the LINE Platform sends a request to our webhook URL. SUSI Server then sends a request to the LINE Platform to respond to the user. Requests are sent over HTTPS in JSON format. Different Message types Image Messages To send images, we need two things, the URLs of original image and smaller preview image in the message object. The preview image will be displayed in text and full image is displayed when user clicks on the preview image. The message object for image message has 3 properties - Property Type Description type String image originalContentUrl String Image URL previewImageUrl String Preview image URL Sample Message object: {    "type": "image",    "originalContentUrl": "https://susi.ai/original.jpg",    "previewImageUrl": "https://susi.ai/preview.jpg" } Video Messages To send videos, we need two things, the URL of video file and URL of preview image of video in the message object. The message object for video messages has 3 properties: Property Type Description type String image originalContentUrl String Video file URL previewImageUrl String Preview image URL Sample Message object for video type responses: {    "type": "video",    "originalContentUrl": "https://susi.ai/original.mp4",    "previewImageUrl": "https://susi.ai/preview.jpg" } Audio Messages To send an audio message, you have to include the URL to audio file and the duration in the message object. The message object for audio messages has 3 properties: Property Type Description type String image originalContentUrl String Audio file URL duration Number Length of the Audio file in milliseconds Sample Message object for audio type responses: {    "type": "audio",    "originalContentUrl": "https://susi.ai/original.m4a",    "duration": 10000 } Location Messages To send location information to users, you need to include title, address, latitude and longitude of the location. The message object needs to include 5 properties: Property Type Required Description type String Required location title String Required Title address String Required Address latitude Decimal Required Latitude longitude Decimal Required Longitude Sample Message object for location type responses: {    "type": "location",    "title": "singapore",    "address": "address",    "latitude": 1.2896698812440377,    "longitude": 103.85006683126556 } How to use these Message objects? You have to send the response that you got from SUSI server to LINE platform. Now this response is sent in the form of an object. This object tells the LINE platform about the type of this message. So simply sending this object to reply API of LINE sends the message to user. It looks like this: return client.replyMessage(event.replyToken, answer); References: LINE Messaging API documentation: https://developers.line.me/en/docs/messaging-api/overview/ LINE Message Types: https://developers.line.me/en/docs/messaging-api/message-types/ SUSI LINE Bot Repository: https://github.com/fossasia/susi_linebot

Continue ReadingHow to receive different types of messages from SUSI Line Bot

How to make SUSI AI Line Bot

In order to integrate SUSI’s API with Line bot you will need to have a line account first so that you can follow below procedure. You can download app from here. Pre-requisites: Line app Github Heroku Steps: Install Node.js from the link below on your computer if you haven’t installed it already https://nodejs.org/en/. Create a folder with any name and open shell and change your current directory to the new folder you created. Type npm init in command line and enter details like name, version and entry point. Create a file with the same name that you wrote in entry point in above given step. i.e index.js and it should be in same folder you created. Type following commands in command line  npm install --save @line/bot-sdk. After bot-sdk is installed type npm install --save express after express is installed type npm install --save request when all the modules are installed check your package.json modules will be included within dependencies portion. Your package.json file should look like this. { "name": "SUSI-Bot", "version": "1.0.0", "description": "SUSI AI LINE bot", "main": "index.js", "dependencies": {   "@line/bot-sdk": "^1.0.0",   "express": "^4.15.2",   "request": "^2.81.0" }, "scripts": {   "start": "node index.js" } } Copy following code into file you created i.e index.js 'use strict'; const line = require('@line/bot-sdk'); const express = require('express'); var request = require("request"); // create LINE SDK config from env variables const config = {   channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN,   channelSecret: process.env.CHANNEL_SECRET, }; // create LINE SDK client const client = new line.Client(config); // create Express app // about Express: https://expressjs.com/ const app = express(); // register a webhook handler with middleware app.post('/webhook', line.middleware(config), (req, res) => {   Promise       .all(req.body.events.map(handleEvent))       .then((result) => res.json(result)); }); // event handler function handleEvent(event) {   if (event.type !== 'message' || event.message.type !== 'text') {       // ignore non-text-message event       return Promise.resolve(null);   }   var options1 = {       method: 'GET',       url: 'http://api.asksusi.com/susi/chat.json',       qs: {           timezoneOffset: '-330',           q: event.message.text       }   };   request(options, function(error, response, body) {       if (error) throw new Error(error);       // answer fetched from susi       //console.log(body);       var ans = (JSON.parse(body)).answers[0].actions[0].expression;       // create a echoing text message       const answer = {           type: 'text',           text: ans       };       // use reply API       return client.replyMessage(event.replyToken, answer);   }) } // listen on port const port = process.env.PORT || 3000; app.listen(port, () => {   console.log(`listening on ${port}`); }); Now we have to get channel access token and channel secret to get that follow below steps. If you have Line account then move to next step else sign up for an account and make one. Create Line account on  Line Business Center with messaging API and follow these steps: In the Line Business Center, select Messaging API under the Service category at the top of the page. Select start using messaging API, enter required information and confirm it. Click LINE@ Manager option, In settings go to bot settings and Enable messaging API Now we have to configure settings. Allow messages using webhook and select allow for “Use Webhooks”. Go to Accounts option at top of page and open LINE Developers. To get Channel access…

Continue ReadingHow to make SUSI AI Line Bot