My First Playground

node v8.15.1
version: 1.0.0
endpointsharetweet
This is a playground to test JavaScript. It runs a completely standard copy of Node.js on a virtual server created just for you. Every one of npm’s 300,000+ packages are pre-installed, so try it out:
var endpoint = require("@runkit/runkit/json-endpoint/1.0.0"); var axios = require('axios'); const StringDecoder = require("string_decoder").StringDecoder; function getHoroscope(messengerID, zodiac, whichDay, res) { if (zodiac) {require("axios") axios.get('http://theastrologer-api.herokuapp.com/api/horoscope/'+zodiac+'/'+whichDay) .then( (response) => { if(response) { console.log(response.data.horoscope); sendMessage(messengerID, response.data, res); } }) .catch(function (error) { console.log(error); }); } }; function sendMessage(messengerID, astrology, response){ var lucky_num = [] while(lucky_num.length < 5){ var r = Math.floor(Math.random()*99) + 1; if(lucky_num.indexOf(r) === -1) lucky_num.push(r.toString()); } var lucky_numbers = lucky_num[0]+" "+lucky_num[1]+" "+lucky_num[2]+" "+lucky_num[3]+" "+lucky_num[4]; var data = { token: "cRdXsDn9y53sppKA4Et84VUoPOOX6irCaoFMd7EaRSjIzNYQ7fh6SGmx62zOs3Vc", chatbot_id: "f69be68e-6832-4e0c-8300-3f0fad7f80f0", component_id: "f4bae0d0-c3b2-40d3-ba7f-d1e9ebcc7b9b", messenger_user_id: messengerID, attributes: [ { name: "horoscope", value: astrology.horoscope.substring(0, 254) }, { name: "horoscope_ext", value: astrology.horoscope.substring(254, 509) }, { name: "date", value: astrology.date }, { name: "sun_sign", value: astrology.sunsign }, { name: "mood", value: astrology.meta.mood }, { name: "intensity", value: astrology.meta.intensity }, { name: "lucky_numbers", value: lucky_numbers } ] } axios.post('https://app.elchatto.com/api/integrations', data) .then(function (res) { const x = res.data['ok'] ? 'OK' : 'NOT OK'; response.end(x); }) .catch(function (error) { response.end("ERROR"); }); } exports.endpoint = function(request, response) { const { headers, method, url } = request; const decoder = new StringDecoder('utf-8'); let payload = ''; request.on('data', (data) => { payload += decoder.write(data); }); request.on('end', () => { payload += decoder.end(); payload = JSON.parse(payload); var messengerID = payload.messenger_user_id; var zodiac = payload.zodiac; var whichDay = payload.when; getHoroscope(messengerID, zodiac, whichDay, response); }); }
Loading…

no comments

    sign in to comment