Skip to content

Instantly share code, notes, and snippets.

@triton11
Created February 21, 2016 16:00
Show Gist options
  • Save triton11/adb65c7d27d99a26ad2c to your computer and use it in GitHub Desktop.
Save triton11/adb65c7d27d99a26ad2c to your computer and use it in GitHub Desktop.

Revisions

  1. triton11 created this gist Feb 21, 2016.
    147 changes: 147 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,147 @@
    //bot.js
    var request = require('request');
    var himalaya = require('himalaya');
    var phys;
    var ment;
    var name;
    var Botkit = require('botkit');
    var res = {}
    var x = ''
    var promise = ''
    var request = require('superagent');
    var jwt = require('jsonwebtoken');
    var USER_ID = 'arelin';
    var KEY_ID = '56c9bd9400bb932900c2198d';
    var SECRET = 'k3DJxQsARESLKDbVqW8cMeXL';
    var token = jwt.sign({
    scope: 'app'
    },
    SECRET,
    {
    headers: {
    kid: KEY_ID
    }
    });


    var controller = Botkit.slackbot({})

    var bot = controller.spawn({
    token: 'xoxb-22354931845-XT1OSyOxeY46SKQ3FT0EpK8v'
    });

    // use RTM
    bot.startRTM(function(err,bot,payload) {
    // handle errors...
    });

    // reply to @bot hello
    //controller.hears('Use ',['direct_message','direct_mention','bot_message','ambient', 'message'],function(bot,message) {
    controller.on('bot_message',function(bot,message) {
    //console.log(message);

    bot.startConversation(message,function(err,convo) {

    request
    .post('https://api.smooch.io/v1/appusers/' + USER_ID + '/conversation/messages')
    .send({
    text: 'Describe your emergency',
    role: 'appMaker'
    })
    .set('authorization', 'Bearer ' + token)
    .set('Accept', 'application/json')
    .end(function(err, res) {
    //console.log(err, res.body, res.statusCode);
    });

    convo.ask('Describe your medical emergency',function(response,convo) {

    phys = JSON.stringify(response);
    ment = phys.match(/"fallback":"([^"]*)/)
    //console.log(response);

    //ment = phys.attachments.fallback;
    });
    });
    bot.closeRTM()
    bot.startRTM(function(err,bot,payload) {
    // handle errors...
    });

    controller.on('bot_message',function(bot,message) {

    bot.startConversation(message,function(err,convo) {
    request
    .post('https://api.smooch.io/v1/appusers/' + USER_ID + '/conversation/messages')
    .send({
    text: 'What is your name?',
    role: 'appMaker'
    })
    .set('authorization', 'Bearer ' + token)
    .set('Accept', 'application/json')
    .end(function(err, res) {
    //console.log(err, res.body, res.statusCode);
    });

    convo.ask('What is your name?',function(response,convo) {
    //phys = JSON.stringify(response);
    //ment = phys.match(/"fallback":"([^"]*)/)
    name = response.text;
    console.log(name);

    var baseUrl = "https://open-ic.epic.com/FHIR/api/FHIR/DSTU2/"
    var patientSearchString = "/Patient?given="+name+"&family=Ragsdale"





    //Lets configure and request
    //Lets try to make a HTTP GET request to modulus.io's website.
    request(baseUrl + patientSearchString, function (error, response, body) {
    if (!error && response.statusCode == 200) {
    promise = himalaya.parse(body);
    //patient = promise.entry[0].resource
    //console.dir(promise, {colors: true, depth: null});
    convo.next();
    x = (promise[0].children[5].children[4].children[0].children[0].attributes.value)
    convo.ask('Is your birthdate ' + x + '?' ,function(response,convo) {
    var ans = response.text;
    if (ans.localeCompare('Yes' == 0)) {
    convo.say('Hello '+name+' Ragsdale.');
    // do something else...
    var id = promise[0].children[5].children[0].attributes.value.split("/").pop();
    console.log(id);
    request(baseUrl + "AllergyIntolerance?patient=" + id, function (error, response, body) {
    if (!error && response.statusCode == 200) {
    med = himalaya.parse(body);
    //console.dir(med, {colors: true, depth: null});
    var cond = med[0].children[5].children[4].children[0].children[6].children[0].attributes.value;
    console.log(cond);
    convo.next();

    convo.say('Based on your medical history you may be reacting to '+ cond +' . Would you like us to call you a doctor?');
    }
    });

    }
    else {
    convo.say('You are not in our database');
    // do something else...
    convo.next();
    }

    });
    //console.dir(promise, {depth: null});


    convo.say('Thank you for your responses. We will return your diagnosis shortly.')
    convo.next();
    }
    });
    });
    });
    });
    })