Function not waiting till everything is executed before moving on - javascript

I am building a chatbot. I have a function as below:
function Smoking(){
var userinput=document.getElementById("messages").value
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Do you smoke?");
if (userinput=="Yes"){
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Oh no! Smoking is not good for your health!");
}else if(userinput=="No"){
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Good to hear that you are not smoking!");
}
}
The Smoking() function is called when user ask the chatbot to ask a question:
var convpatterns = [
[".*hi.*", "Hello there!","Greetings!"],
[".*ask me.*", Smoking],
["Yes","You seem quite sure.","OK, but can you elaborate a bit?"]
The window.iSpeech.speak plugin allow the system to speak the words and the plugin is installed correctly and works in other functions. However when the Smoking() function is called, the system only speak "Do you smoke?", and doesn't wait for user input. When user typed in the textbox "Yes" or "No", the system moves on and recognise the input text as the "Yes" array instead of the "Yes" within the Smoking() function. So if the user typed "Yes", the chatbot will say "You seem quite sure", instead of "Oh no! Smoking is not good for your health!"
So I was just wondering if there is a way for the function to wait till all lines are executed before moving on?
UPDATE
I've tried putting them into two separate function but it is still not working:
function Smoking(){
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Do you smoke?");
SmokingAnswer()
}
function SmokingAnswer(){
var userinput=document.getElementById("messages").value
if (userinput=="Yes"){
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Oh no! Smoking is not good for your health!");
}else if(userinput=="No"){
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Good to hear that you are not smoking!");
}
}
The SmokingAnswer() function is not executed.

The way I understand it is, you want to ask the question to the user by voice, and the user should reply to it in the chat box.
For your application to work the proper way, you should separate it into multiple JS functions.
First function gets called when user enters the site, or focuses the chat box. This function will ask the question.
Then the user enters something, and lets say the function second runs after the user clicks a button. The second function would then check the input and evaluate the possible answer. "oh god no ..." / "good to hear..."
The way your function is designed, is this: once it runs it will ask the question, but it needs the answer already in the chatbox, which at that point is most probably empty since you havent asked the user yet.

Related

Webpage redirect to submitted text value

I want to create a static webpage which has a text field and a submit button, which after a user submits text it would automatically redirect from localhost/ to localhost/<test_person_submitted> .
Up to this point, this JavaScript is the only thing I was able to clobber together from all of the guides I found
function confirmInput() {
fname = document.forms[0].fname.value;
url = "http://localhost/"
command = (url + fname)
location.replace(command);
}
That does work, but no matter what I added as HTML to invoke the function it doesn't work properly.
Can someone please add a submit form that will properly execute this?
Right now I'm stumped and I just want to consider this small project "complete", rather to just leaving it without ever knowing how it could be resolved.
If you are wondering why I want to do this it's because I'm going to try to invoke some wild-card commands that will return JSON.
And this is just a fun project for me.

Embedded data in Qualtrics showing up in the 'Data & Analysis' tab, but is not showing up for participants

So I have a bit of JS in my Qualtrics survey that takes the response to a question, and if that response is not empty, it will embed the participant’s response (to be called later in the survey). If they don’t enter anything, I have made it print a string. I have done this with the following:
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
var tboxID= "QR~"+this.questionId;
var hometownStr = document.getElementById(tboxID).value;
Qualtrics.SurveyEngine.setEmbeddedData('hometownStr',hometownStr);
if(!hometownStr || 0 === hometownStr.length)
{
Qualtrics.SurveyEngine.setEmbeddedData('hometownStr', "your hometown");
}
});
After the block this question is in I have embedded the data.
So, let's say I am asked the question (in which the JS is embedded): What is your hometown? I either answer that my hometown is (for example) "Paisley", or I leave it blank. If I don't answer the question, the next question will say This is a person from your hometown. If I
do answer the question, it should print: This is a person from Paisley.
However, the latter example does not print "Paisley" (it prints nothing). FYI, the former example works fine.
The strange thing is that, though "Paisley" is not printed in the survey, it comes out in the final data (found in the Data & Analysis tab).
Note: I am calling the embedded data by ${e://Field/hometownStr}.
Does anyone know why this might be happening? Thanks in advance for any help; please tell me I am stupid and overlooking something major, and this isn't a bug in Qualtrics...
FYI, this code has worked in the past, so I don't think it's an issue with the code. I refactored some of the questions and blocks and it just stopped working.
Move the survey flow embedded data block BEFORE the block where the JS sets it.
EDIT:
I noticed you are using addOnUnload. Use addOnPageSubmit instead.

Discord bot conversation

I'm creating a discord bot that will run a function based on questions asked, of which multiple are yes/no questions. Answering "yes" will do a certain function while "no" should end the question.
I do not want the bot to run a random function every time it reads "yes".
The only solution I could think of is once the question has been asked (based on a word trigger), the user gets stuck in a loop where the bot will not respond to any trigger other than "yes" / "no".
If yes, the related function will run.
If no, the user gets out of the loop and is able to trigger another question.
Other than yes/no, the bot will say "please reply with yes or no"
How can I code this in node.js/discord.js?
If this is a command that the users must run to initiate this conversation...I recommend referencing this in the discord.js official documentaion that talks about awaitMessages. Basically, you could use this followed by a series of .then() and if statements that will take the user down multiple paths based on their answers that can be filtered out.
If this is a default function of the bot without needing a trigger command, I recommend you tread carefully in this area as something like this could be quite spammy.
An example:
message.channel.send('Is this a great bot?').then(async (start) => {
message.channel.awaitMessages(filter, { maxMatches: 1, time: 60000, errors: ['time']}).then(async (collected) => {
if (collected.first().content === "yes") {} else if (collected.first().content === "no") {}
})
You can filter messages with the filter or another way so that yes/no are the only acceptable answers.

javascript- Wait till function finish before continuing

I'm building a chatbot. Some example of the chat scripts are as follows:
var convpatterns = new Array (
new Array (".*ask me a question*", Question1),
new Array ("I need (.*)" , "Why do you need $1?" , "Are you sure you need $1?"),
new Array (".*sorry.*", "Please dont apologise", "Apologies are not necessary", "it is ok, it didn't bother me")
);
So basically if the user types "ask me a question", it will direct the user to the Question1() function. And if the user types "I need a friend", the chatbot will reply by asking "why do you need a friend?" or "Are you sure you need a friend?".
function Question1(){
var textbox=document.getElementById("messages").value;
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Do you smoke?");
if (textbox="Yes"){
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Oh, do you know smoking is bad for your health?");
} else if (textbox="No"){
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"That's great to hear that you don't smoke!");
}
}
The window.ispeech.speak will allow the chatbot to verbally speak the words.
So when the chatbot asks the question, "Do you smoke?", and the user types either "yes" or "no" in the textbox, the chatbot then responds based on the reply.
What I want is for the Question1() function to finish running before the chatbot goes about and asks other things (so it waits for the user to type either "yes" or "no" before the function can be finished and a new script can begin), as I don't want the user's response to clash with the other scripts available in the arrays (e.g. the input "yes" to the textbox might clash with another "Yes" array in the script).
You actually need something like a state machine. But can always hold the current -let's say- command in a variable. Then you always check for the command to be finished. As long as a current command is not set to null(means no current command) you can not listen to another command. Each of you command should have at least one final state. It can be a value that will be entered by user. When the final state condition(s) of current command were meet you can set it to null.

Reading and checking user's entry from a text field in an html form

What I want to do is to have a form field that allows a person to try to guess from a picture what type of bird it is, and if they get it right, it tells them they got it right and gives them the code to be able to get a discount.
Here is the code I'm using within the head tags:
formCheck()
{
var birdName = document.forms[0].birdName.value
if (birdName == "red bellied woodpecker")
alert("That's Correct! Please enjoy 10% off your next purchase by entering the code NAMETHATBIRD92 during checkout.")
else
alert("That's isn't the correct answer! Make sure your answer is very specific and keep trying, you can guess as many times as you want.")
}
Here is what I have within the body tag:
Can you name this bird?
It works here:
www.madhatwebsolutions.com/namethatbird.html
It does not work here, where I really need it to work:
http://www.wildbirdsmarketplace.com/pages/Name-That-Bird!.html
This shouldn't be JavaScript.
Any potential customer will be able to right click and view your JavaScript source and retrieve the code without bothering with the guesswork.
You'll need to query a server with the user input, and the server will need to return a response indicating whether this input is correct or not.
You might want to look at either a normal HTML form submission, or venture into AJAX
Workflow:
User enters guess into textfield
Launch a request to http://yourserver.com/check_bird.your_server_language?guess=theTextFieldValue
Server returns either a success or failure indication
Display response to client
Other things to consider: Are you going to allow your customers to guess multiple times, or restrict them? Are you going to be showing several different birds or not?
in http://www.wildbirdsmarketplace.com/pages/Name-That-Bird!.html
<script type="text/javascript" src="birdname.js"></script> refers to 404 - check the file path
don't use document.forms
var birdName = document.getElementById('birdName').value;

Categories

Resources