From d13b68817b5902634c0c60c14e1116f37059613c Mon Sep 17 00:00:00 2001 From: Manveer Singh Date: Tue, 5 Sep 2017 20:56:54 +0530 Subject: [PATCH] How to contribute button added (#45) --- index.js | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b7eaf33..e53711b 100644 --- a/index.js +++ b/index.js @@ -140,6 +140,15 @@ app.post('/', function(req, response) { "TextSize": "large", "TextVAlign": "middle", "TextHAlign": "middle" + },{ + Columns: 6, + Rows: 1, + Text: "How to contribute?", + "ActionType": "reply", + "ActionBody": "Contribution", + "TextSize": "large", + "TextVAlign": "middle", + "TextHAlign": "middle" }]; var options2 = { @@ -153,7 +162,7 @@ app.post('/', function(req, response) { rich_media: { Type: "rich_media", ButtonsGroupColumns: 6, - ButtonsGroupRows: 2, + ButtonsGroupRows: 3, BgColor: "#FFFFFF", Buttons: buttons } @@ -258,6 +267,146 @@ app.post('/', function(req, response) { }); }); } + else if(message === "Contribution"){ + // setting options to request susi bot. + var options1 = { + method: 'GET', + url: 'http://api.susi.ai/susi/chat.json', + qs: { + timezoneOffset: '-330', + q: message + } + }; + + // A request to the Susi bot + request(options1, function(error1, response1, body1) { + if (error1) throw new Error(error1); + // answer fetched from susi + ans = (JSON.parse(body1)).answers[0].actions[0].expression; + + var options = { + method: 'POST', + url: 'https://chatapi.viber.com/pa/send_message', + headers: headerBody, + body: { + receiver: req.body.sender.id, + min_api_version: 1, + tracking_data: 'tracking data', + type: 'text', + text: ans + }, + json: true + }; + + // request to the chat api of viber. + request(options, function(error, res, body) { + if (error) throw new Error(error); + + var buttons = [{ + Columns: 6, + Rows: 1, + Text: "Visit repository", + "ActionType": "open-url", + "ActionBody": "https://www.github.com/fossasia/susi_server", + "TextSize": "large", + "TextVAlign": "middle", + "TextHAlign": "middle" + }]; + + var options2 = { + method: 'POST', + url: 'https://chatapi.viber.com/pa/send_message', + headers: headerBody, + body: { + receiver: req.body.sender.id, + min_api_version: 2, + type: 'rich_media', + rich_media: { + Type: "rich_media", + ButtonsGroupColumns: 6, + ButtonsGroupRows: 1, + BgColor: "#FFFFFF", + Buttons: buttons + } + }, + json: true + }; + + request(options2, function(error2, res2, body2) { + if (error) throw new Error(error); + + var options4 = { + method: 'GET', + url: 'http://api.susi.ai/susi/chat.json', + qs: { + timezoneOffset: '-330', + q: 'Gitter channel' + } + }; + + // A request to the Susi bot + request(options4, function(error1, response1, body1) { + if (error1) throw new Error(error1); + // answer fetched from susi + ans = (JSON.parse(body1)).answers[0].actions[0].expression; + + var options5 = { + method: 'POST', + url: 'https://chatapi.viber.com/pa/send_message', + headers: headerBody, + body: { + receiver: req.body.sender.id, + min_api_version: 1, + tracking_data: 'tracking data', + type: 'text', + text: ans + }, + json: true + }; + + // request to the chat api of viber. + request(options5, function(error, res, body) { + if (error) throw new Error(error); + + var buttons = [{ + Columns: 6, + Rows: 1, + Text: "Chat on Gitter", + "ActionType": "open-url", + "ActionBody": "https://www.gitter.im/fossasia/susi_server", + "TextSize": "large", + "TextVAlign": "middle", + "TextHAlign": "middle" + }]; + + var options6 = { + method: 'POST', + url: 'https://chatapi.viber.com/pa/send_message', + headers: headerBody, + body: { + receiver: req.body.sender.id, + min_api_version: 2, + type: 'rich_media', + rich_media: { + Type: "rich_media", + ButtonsGroupColumns: 6, + ButtonsGroupRows: 1, + BgColor: "#FFFFFF", + Buttons: buttons + } + }, + json: true + }; + + request(options6, function(error2, res2, body2) { + if (error) throw new Error(error); + }); + }); + }); + }); + }); + }); + } else{ // setting options to request susi bot. var options1 = {