I used java to make a pretty neat chatbot a while ago. Recently, I discovered I could use VBS to read text out loud. I used what I discovered to have the chatbot actually speak instead of just printing the response.
That all works fine, but the user can type responses faster than the text-to-speech can finish talking. This causes a backup, as the user enters messages, the bot's responses just get added to the tts queue and won't be spoken until the first response(s) are finished being read.
When the script to read the response is called, I want all speech to be stopped before the response is read. I don't have a clue as how to do this, help would be appreciated. Thanks!
textSpeech.vbs
//I want all speech to be stopped here
Dim sapi
Set sapi=CreateObject("sapi.spvoice")
//speaks the string passed to script
sapi.Speak Wscript.Arguments(0)
Chatbot.java (only relevant code is shown)
try {
//textSpeech.vbs is executed with sayString as an argument
Runtime.getRuntime().exec( "wscript \"" + path + "\" \"" + sayString + "\"");
} catch( IOException e ) {
System.out.println(e);
System.exit(0);
}
VBS has just one thread, it means that once you start a process, the processor stops at that line and does not go forward in your code til the process is finished.
Inside vbs you cant do anything. But, for our luck, you can do this at java.
When users start typing again, you kill the runtime process from java:
Process rp = Runtime.getRuntime()
.exec( "wscript \"" + path + "\" \"" + sayString + "\"");
// whenever user starts typing{
if(user.isTyping()){
rp.destroy();
}
I hope it works for you!
Related
I write discord bot with using JDA and i have one important question how to download attachments or work with them? Because my IntelliJ say "Deprecated API usage" for method like attachment.downloadToFile("name.png);
So now we shouldn't download users files send in message? Or how we should do it in good way? I search a lot of wiki from JDA and different posts, but everywhere i didn't see, a new option to handle this download files, becasue all methods to download, are "Deprecated API" even method like "attachment.retrieveInputStream().join()" retrieveInputStream its too not good way :(
Search a lot on wiki/others pages for more information but nothing found :(
The deprecation notice says this:
Deprecated.
Replaced by getProxy(), see FileProxy.downloadToFile(File)
This means you should use attachment.getProxy().downloadToFile(File) instead.
Example:
attachment.getProxy().downloadToFile(new File("myimage.png")).thenAccept(file -> {
System.out.println("Written to file " + file.getAbsolutePath() + ".");
});
Or using NIO instead:
attachment.getProxy().downloadToPath().thenAccept(path -> {
System.out.println("Written to file " + path + ". Total size: " + Files.size(path));
});
I have a problem with writing down the data I recieve through my C# server in an Android app. The app sends the server the function to give back a list of all running processes.
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
sw.Write(p.ProcessName + "\n");
sw.Flush();
Console.Write(p.ProcessName + "\n");
}
The Console output works perfectly fine, but at the recieving end I just get the first process in the list. The problem lies in the \n: I tried first building up a string, then flushing and with for-loops printing numbers. It's always the \n that fails.
I'm printing in out into a TextView, if that helps.
Thanks for any suggestions.
Instead of sending "\n" to your client send "%d%n%s%n" Source: \n won't work, not going to a new line
Last option would be instead of sending the "\n" from the server you could write a byte and then on the android side you could interpret it as
a "\n"
if(DataInputStream.readByte()==1){//Replace the 1 with whatever byte you want to represent the \n.
runOnUiThread(new Runnable(){//Runs the code on the UiThread
public void run()
{
MainActiviy.TextView.append("\n");//When you get the byte you append a \n to your textview
}
});
}
You would run this piece of code on a Network thread and it would update the Ui.
I've set up my webview properly and I execute my Javascript in the onPageFinished method.
The problem is that whenever I try to change the value of my input, no matter what I do, I end up with the entire webview being replaced by the value of the input that I'm trying to replace.
view.loadUrl("javascript:document.forms[0].elements['password'].value = 'test';");
Any ideas?
PS. This code in the same place works perfectly.
view.loadUrl("javascript:checkForm()");
This executes the checks and submits the form. I get a popup "Please enter a value" after running this.
I have also tried document.getElementsByName("password")[0].value = 'test'; Same result.
RESOLVED: It was the emulator causing the problem. I tested directly on my phone with USB debugging switched on and it works perfectly.
Your first line of code works for me using id or name attributes:
view.loadUrl("javascript:document.forms[0].elements['password'].value = 'test';");
the second line, works with getElementById but doesn't with getElementByName:
view.loadUrl("javascript:document.getElementByName('password').value = 'test';");
So, that's not the problem. Maybe if you post the rest of the code.
You try to change the view, so you need to use:
.dispatchEvent(new Event('input')) ",null).
Try this:
view.evaluateJavascript("javascript: " +
"var password= document.getElementById('password'); " +
"password.value = 'test'; " +
"password.dispatchEvent(new Event('input')) ",null);
I have a college assignment where I have to get a first, middle, and last name from the user, and their age, and give them a bank ID using the initials and whatnot. But that's not what I'm here to ask.
I wanted to have a little bit of fun with it, and make the "Imaginary Bank" accidentally tell the user that it's a scam! Then an Error will pop up and delete that accidental line of text, replacing it with the normal "We look forward to helping you!" line. All I need to know how to do is delete that line of text that starts with "At Imaginary Bank, we" Thanks!
System.out.println("Hello " + first_name + " " + last_name + ", greetings from the Imaginary Bank!");
System.out.println("To access your account, please use the following ID: " + first_init + middle_init + last_init + age);
System.out.println("At Imaginary Bank we look forward to scamming you and stealing your money!");
try
{
Thread.sleep(11000);
}
catch (InterruptedException e) {
e.printStackTrace();}
System.out.println("ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR!");
try
{
Thread.sleep(3000);
}
catch (InterruptedException e) {
e.printStackTrace() ;}
System.out.println("We look forward to aiding you with your financial needs! - The IB Team");
According to this answer, you can print a backspace character using \b in the console using System.out.print. Therefore, for however many characters you have previously printed, print that many backspace characters.
Additionally, this answer for the same question suggests using the cls command to clear the console output entirely, however this forever binds your application to only operating systems that use that command (In this case Windows / Dos). In linux, for example, the command is clear...I'm sure you see the potential problem.
You can always print out backspace characters like this:
System.out.print("\b");
Just print out the same number of characters you would like to remove.
Check out How to delete stuff printed to console by System.out.println()? There is not a certain way to remove text from the output window but there is generally a way for each type of console window. Take your pick for what works best for your deployment.
Please don't hesitate to edit the question or ask more details if I missed anything.
I know it's bad to use Scriptlets in JSP.
But I am assigned to maintain the existing JAVA project which is build only with only JSP and servlets(No framework).
My task is to implement the load balancing for my applicaiton using Apache HTTP Server.
The application works fine with out load balancing. When I implement the load balancing using the Apache HTTP Server, I am facing the problem with JSP.
I will give a scenario. My JSP has one while loop and it runs the javascript to update the content .
My JSP has,
<%
String jsPreAppend = "<script language=JavaScript >push('";
String jsPostAppend = "')</script> ";
String s=null;
int i = 0;
try {
while (true) {
System.out.println("count :"+i);
out.print(jsPreAppend + i + jsPostAppend);
out.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
out.print(jsPreAppend + "InterruptedException: " + e + jsPostAppend);
}
i++;
}
} catch (Exception e) {
out.print(jsPreAppend + "Exception: " + e + jsPostAppend);
}
%>
My JavaScript has,
function push(content) {
document.getElementById('update').innerHTML = content;
}
The console output will be,
count :1
count :2
count :3
.
.
.
count : n
count :n+1
But the content will not updated in JSP. I thing the javascript fails in while loop.
But the SysOut() works because the updated content will be printed for every sec in the console .
But the same applicaiton work's fine with out load balancing(only one tomcat).
Hope our stack users will help me.
When your HTML gets rendered, JSP would have already got executed. So what you are trying to do cannot be achieved by that code.
You need to write a Java script method which does some update once in sometime. Check this thread to write the same logic using Javascript
Take into account that the while(true) loop will be executed server side. At that point, the response document (the HTML) is being built, and it can't be yet interpreted by the client. This loop is only writing javascript calls to a kind of buffer where the response is stored before it is sent to the client.
As an example, what that loop is doing is writing ad-infinitum to the response:
<script language=JavaScript >push('1')')</script>
...
<script language=JavaScript >push('n')')</script>
The fact that every line is being written at every second is irrelevant. You see the traces in the standard output at the correct times because that's what is being executed on the server.
This will make the request get stuck in that infinite loop unless there's an exception of some kind. Even if the loop ended at some point, and the request finished processing, when these statements would get executed by a client, they would be executed sequentially without any delay.
You should move those calls to client side, and schedule its execution with a client-side mechanism such as setTimeout(), like #sanbhat suggested in his answer.