Giter Site home page Giter Site logo

talgenkin / idd-fa18-lab6 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from far-lab/idd-fa19-lab6

0.0 1.0 0.0 2.41 MB

A simple chat bot to teach about web-sockets, web-servers and the difficulties to create a meaning-full conversation.

License: Other

JavaScript 85.02% CSS 6.97% HTML 8.01%

idd-fa18-lab6's Introduction

ChatBot

A lab report by Tal Genkin

Make the ChatBot your own

I change the questions to be trivia questions. The user gets three questions: what is the capital of spain, how much is 2 in the power of 1024 and in what year was the airplane invented. If the user answers correctly, s/he gets a message that the answer was correct, and otherwise, that the answer wasn't correct, and gets the correct answer. I did leave the font change at the end and just changed the quetion.

The code - used by me

Also, can be seen here below:

/* chatServer.js Author: David Goedicke ([email protected]) Closley based on work from Nikolas Martelaro ([email protected]) as well as Captain Anonymous (https://codepen.io/anon/pen/PEVYXz) who forked of an original work by Ian Tairea (https://codepen.io/mrtairea/pen/yJapwv) */

var express = require('express'); // web server application var app = express(); // webapp var http = require('http').Server(app); // connects http library to server var io = require('socket.io')(http); // connect websocket library to server var serverPort = 8000;

//---------------------- WEBAPP SERVER SETUP ---------------------------------// // use express to create the simple webapp app.use(express.static('public')); // find pages in public directory

// start the server and say what port it is on http.listen(serverPort, function() { console.log('listening on *:%s', serverPort); }); //----------------------------------------------------------------------------//

//---------------------- WEBSOCKET COMMUNICATION -----------------------------// // this is the websocket event handler and say if someone connects // as long as someone is connected, listen for messages io.on('connect', function(socket) { console.log('a new user connected'); var questionNum = 0; // keep count of question, used for IF condition. socket.on('loaded', function() { // we wait until the client has loaded and contacted us that it is ready to go.

socket.emit('answer', "Hey there! Hello I am \"___*-\" a simple chat bot example."); //We start with the introduction;
setTimeout(timedQuestion, 5000, socket, "What is your name?"); // Wait a moment and respond with a question.

}); socket.on('message', (data) => { // If we get a new message from the client we process it; console.log(data); questionNum = bot(data, socket, questionNum); // run the bot function with the new message }); socket.on('disconnect', function() { // This function gets called when the browser window gets closed console.log('user disconnected'); }); }); //--------------------------CHAT BOT FUNCTION-------------------------------// function bot(data, socket, questionNum) { var input = data; // This is generally really terrible from a security point of view ToDo avoid code injection var answer; var question; var waitTime;

/// These are the main statments that make up the conversation. if (questionNum == 0) { answer = 'Hello ' + input + ' :-)'; // output response waitTime = 5000; question = 'How old are you?'; // load next question } else if (questionNum == 1) { answer = 'Really, ' + input + ' years old? So that means you were born in: ' + (2018 - parseInt(input)); // output response waitTime = 5000; question = 'Where do you live?'; // load next question } else if (questionNum == 2) { answer = 'Cool! I have never been to ' + input + '.'; waitTime = 5000; question = 'Whats your favorite color?'; // load next question } else if (questionNum == 3) { answer = 'Ok, ' + input + ' it is.'; socket.emit('changeBG', input.toLowerCase()); waitTime = 5000; question = 'Can you still read the font?'; // load next question } else if (questionNum == 4) { if (input.toLowerCase() === 'yes' || input === 1) { answer = 'Perfect!'; waitTime = 5000; question = 'Whats your favorite place?'; } else if (input.toLowerCase() === 'no' || input === 0) { socket.emit('changeFont', 'white'); /// we really should look up the inverse of what we said befor. answer = '' question = 'How about now?'; waitTime = 0; questionNum--; // Here we go back in the question number this can end up in a loop } else { question = 'Can you still read the font?'; // load next question answer = 'I did not understand you. Could you please answer "yes" or "no"?' questionNum--; waitTime = 5000; } // load next question } else { answer = 'I have nothing more to say!'; // output response waitTime = 0; question = ''; }

/// We take the changed data and distribute it across the required objects. socket.emit('answer', answer); setTimeout(timedQuestion, waitTime, socket, question); return (questionNum + 1); }

function timedQuestion(socket, question) { if (question != '') { socket.emit('question', question); } else { //console.log('No Question send!'); }

} //----------------------------------------------------------------------------//

Record someone trying out your ChatBot

Using a phone or other video device, record someone trying out your ChatBot. Upload that video to this repository and link to it here!

Trivia game

A user using the code


Starter code by David Goedicke, closely based on work by Nikolas Martelaro and Captain Anonymous, who forked original work by Ian Tairea.

idd-fa18-lab6's People

Contributors

davidgoedicke avatar zamfi avatar talgenkin avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.