I'm using Twilio in Java to send or receive text messages. I added twilio-7.47.1-jar-with-dependencies.jar to the project Java library and executed the following syntax based on an example in Twilio's website.
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
public class Example {
public static final String ACCOUNT_SID = "AC41b4ea83f681353d261d5d0997d73b30";
public static final String AUTH_TOKEN = "my_auth";
public static void main(String[] args) {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Message message = Message.creator(
new com.twilio.type.PhoneNumber("+15558675310"),
new com.twilio.type.PhoneNumber("+15017122661"),
"This is the ship that made the Kessel Run in fourteen parsecs?")
.create();
System.out.println(message.getSid());
}
}
I have a syntax error with the creator method and it is: The method creator(String) in the type Message is not applicable for the arguments (PhoneNumber, PhoneNumber, String)
I looked at many websites related to this subject and all of them use the same syntax for this task.
Does anyone have any idea how to fix it?
Many thanks!
Related
Here's my code in Bot.java.
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import javax.security.auth.login.LoginException;
public class Bot {
private JDA api;
public Bot() throws LoginException, InterruptedException {
api = JDABuilder.createDefault("token")
.addEventListeners(new search()).build()
.awaitReady();
}
public static void main(String[] args) throws LoginException, InterruptedException {
new Bot();
}
}
token is replaced with an actual token in my code :)
And here's my code in search.java
package core;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public class search extends ListenerAdapter {
#Override
public void onMessageReceived(MessageReceivedEvent event) {
String msg = event.getMessage().getContentRaw();
if (event.getAuthor().isBot())
return;
if (msg.equalsIgnoreCase("!search")) {
event.getPrivateChannel()
.sendMessage("Please enter the course name: ").queue();
}
}
}
I'm not sure why my bot doesn't send the appropriate message when I put search to my discord channel (tried both private and guild).
Please let me know if you know where the problem is, thank you!
The Bot's intents/permissions need to be set in the createDefault method (and possibly inside the discord developer portal). For direct messages it'd be:
JDABuilder.createDefault("token", GatewayIntent.DIRECT_MESSAGES)
To handle messages in your guild, I think it'd require Message Content Intent enabled in the discord developer portal; and it'd be:
JDABuilder.createDefault("token", GatewayIntent.GUILD_MESSAGES)
The GatewayIntent parameter inside the createDefault method uses varargs, so you can tack on any number of intents:
JDABuilder.createDefault("token", GatewayIntent.DIRECT_MESSAGES, GatewayIntent.GUILD_MESSAGES)
Also, event.getPrivateChannel() will throw an IllegalStateException in the event that the message is not from a private channel; I'd recommend first checking if event.getChannelType() == ChannelType.PRIVATE, or a try/catch.
I am trying to make my first discord bot using discord and my code does not want to work. JDABuilder class is working after I added an external library for it. But AccountType is not working.[enter image description here.
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDABuilder;
public class Core {
public static void main(String[] args){
JDABuilder builder = new
JDABuilder(AccountType.BOT);
string token = "Token was here";
builder.setToken(token);
}
}
I am trying to use Amazon Polly to convert text to speech using Java API. As described by Amazon there are several US english voices which support Neural. https://docs.aws.amazon.com/polly/latest/dg/voicelist.html
The code I am following to run in Java application is as following:
package com.amazonaws.demos.polly;
import java.io.IOException;
import java.io.InputStream;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.polly.AmazonPollyClient;
import com.amazonaws.services.polly.model.DescribeVoicesRequest;
import com.amazonaws.services.polly.model.DescribeVoicesResult;
import com.amazonaws.services.polly.model.OutputFormat;
import com.amazonaws.services.polly.model.SynthesizeSpeechRequest;
import com.amazonaws.services.polly.model.SynthesizeSpeechResult;
import com.amazonaws.services.polly.model.Voice;
import javazoom.jl.player.advanced.AdvancedPlayer;
import javazoom.jl.player.advanced.PlaybackEvent;
import javazoom.jl.player.advanced.PlaybackListener;
public class PollyDemo {
private final AmazonPollyClient polly;
private final Voice voice;
private static final String JOANNA="Joanna";
private static final String KENDRA="Kendra";
private static final String MATTHEW="Matthew";
private static final String SAMPLE = "Congratulations. You have successfully built this working demo of Amazon Polly in Java. Have fun building voice enabled apps with Amazon Polly (that's me!), and always look at the AWS website for tips and tricks on using Amazon Polly and other great services from AWS";
public PollyDemo(Region region) {
// create an Amazon Polly client in a specific region
polly = new AmazonPollyClient(new DefaultAWSCredentialsProviderChain(),
new ClientConfiguration());
polly.setRegion(region);
// Create describe voices request.
DescribeVoicesRequest describeVoicesRequest = new DescribeVoicesRequest();
// Synchronously ask Amazon Polly to describe available TTS voices.
DescribeVoicesResult describeVoicesResult = polly.describeVoices(describeVoicesRequest);
//voice = describeVoicesResult.getVoices().get(0);
voice = describeVoicesResult.getVoices().stream().filter(p -> p.getName().equals(MATTHEW)).findFirst().get();
}
public InputStream synthesize(String text, OutputFormat format) throws IOException {
SynthesizeSpeechRequest synthReq =
new SynthesizeSpeechRequest().withText(text).withVoiceId(voice.getId())
.withOutputFormat(format);
SynthesizeSpeechResult synthRes = polly.synthesizeSpeech(synthReq);
return synthRes.getAudioStream();
}
public static void main(String args[]) throws Exception {
//create the test class
PollyDemo helloWorld = new PollyDemo(Region.getRegion(Regions.US_WEST_1));
//get the audio stream
InputStream speechStream = helloWorld.synthesize(SAMPLE, OutputFormat.Mp3);
//create an MP3 player
AdvancedPlayer player = new AdvancedPlayer(speechStream,
javazoom.jl.player.FactoryRegistry.systemRegistry().createAudioDevice());
player.setPlayBackListener(new PlaybackListener() {
#Override
public void playbackStarted(PlaybackEvent evt) {
System.out.println("Playback started");
System.out.println(SAMPLE);
}
#Override
public void playbackFinished(PlaybackEvent evt) {
System.out.println("Playback finished");
}
});
// play it!
player.play();
}
}
By default its taking the Standard of the voice of Matthew. Please suggest what needs to be changed to make the speech Neural for the voice of Matthew.
Thanks
Thanks #ASR for your feedback.
I was able to find the engine parameter as you suggested.
The way I had to solve this is:
Update the aws-java-sdk-polly version from 1.11.77 (as they have in their documentation) to the latest 1.11.762 in the pom.xml and build the Maven project. This brings the latest class definition for SynthesizeSpeechRequest Class. With 1.11.77 I was unable to see withEngine function in its definition.
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-polly</artifactId>
<version>1.11.762</version>
</dependency>
Updated the withEngine("neural") as below:
SynthesizeSpeechRequest synthReq =
new SynthesizeSpeechRequest().withText(text).withVoiceId(voice.getId())
.withOutputFormat(format).withEngine("neural");
As defined in https://docs.aws.amazon.com/polly/latest/dg/NTTS-main.html Neural voice is only available in specific regions. So I had to chose as following:
PollyDemo helloWorld = new PollyDemo(Region.getRegion(Regions.US_WEST_2));
After this Neural voice worked perfectly.
I am assuming you are using AWS Java SDK 1.11
AWS documentation here states that you need to set the engine parameter in the speech sysnthesis request to neural. AWS Java SDK documentation here describes the withEngine method to set it to neural.
PS: the documentation page doesn't seem to provide the method URLs, so you will have to search for it.
When I want to run the Java code of the twilio API with IntelliJ, it gives me an error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonMappingException".
I have already added the twilio-7.14.5.jar to the module's dependencies.
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
public class main
{
public static final String ACCOUNT_SID = "AC935209d3c44660b4a550e3380249857a";
public static final String AUTH_TOKEN = "42bcd28e23344404c737eb3499d2a747";
public static void main(String[] args) {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Message message = Message.creator(new PhoneNumber("+13195120377"),
new PhoneNumber("+13193204088"),
"The temperature is over heat now!").create();
}
}
screen shot for the console
You need to include all the dependencies used by Twilio 7.4.15 version. The maven repository lists all the required dependencies http://mvnrepository.com/artifact/com.twilio.sdk/twilio/7.14.5
I'm trying to follow this tutorial, regarding using Java with iMacros, but it isn't working. I'm getting an error saying "Could not find or load the main class" even though there is clearly a main class in the sample code on the website. Does anyone know why this might be happening? Thanks!
Update: Code sample
import com.jacob.activeX.*;
public class iMacroJACOBtest {
public static void main(String[] args) {
// Connect to iMacros Scripting Interface
System.out.println("Started.");
ActiveXComponent iim = new ActiveXComponent("imacros");
System.out.println("Calling iimInit");
// call iimInit()
iim.invoke("iimInit");
// call iimPlay()
System.out.println("Calling iimPlay");
iim.invoke("iimPlay", "CODE:URL GOTO=google.de");
}
}