Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/compile.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#!/bin/sh

SDK_VERSION=$(cd .. && mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)

export SDK_VERSION="${SDK_VERSION}"

(cd snippets && ./compile.sh) || exit 1
(cd webhooks && ./compile.sh) || exit 1
(cd getting-started && ./compile.sh) || exit 1
18 changes: 18 additions & 0 deletions examples/getting-started/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

DIRECTORIES="
conversation/send-text-message
numbers/rent-and-configure
numbers/rent-first-available-number
numbers/search-available
sms/send-sms-message
sms/respond-to-incoming-message
verification/user-verification-using-sms-pin
voice/respond-to-incoming-call
voice/make-a-call
"

for DIRECTORY in $DIRECTORIES
do
(cd "$DIRECTORY" && echo "$PWD" && mvn -Puse-version clean package) || exit 1
done
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Sinch Getting started

Code is related to [Send a Conversation Message with the Sinch Java SDK](https://developers.sinch.com/docs/conversation/getting-started#4-send-the-message).

See [Client template README](../../../client/README.md) for details about configuration and usage.
94 changes: 94 additions & 0 deletions examples/getting-started/conversation/send-text-message/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>my.company.com</groupId>
<artifactId>sinch-java-sdk-client-application</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Sinch Java SDK Client Application</name>

<profiles>
<profile>
<id>use-version</id>
<properties>
<sinch.sdk.java.version>${env.SDK_VERSION}</sinch.sdk.java.version>
</properties>
</profile>
</profiles>

<properties>
<sinch.sdk.java.version>[2.0.0,)</sinch.sdk.java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.version>3.13.0</maven.compiler.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>add-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>
Application
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.sinch.sdk</groupId>
<artifactId>sinch-sdk-java</artifactId>
<version>${sinch.sdk.java.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>2.0.9</version>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import com.sinch.sdk.SinchClient;
import conversation.ConversationQuickStart;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public abstract class Application {

private static final String LOGGING_PROPERTIES_FILE = "logging.properties";
private static final Logger LOGGER = initializeLogger();

public static void main(String[] args) {
try {

SinchClient client = SinchClientHelper.initSinchClient();
LOGGER.info("Application initiated. SinchClient ready.");

ConversationQuickStart conversation = new ConversationQuickStart(client.conversation().v1());

} catch (Exception e) {
LOGGER.severe(String.format("Application failure: %s", e.getMessage()));
}
}

static Logger initializeLogger() {
try (InputStream logConfigInputStream =
Application.class.getClassLoader().getResourceAsStream(LOGGING_PROPERTIES_FILE)) {

if (logConfigInputStream != null) {
LogManager.getLogManager().readConfiguration(logConfigInputStream);
} else {
throw new RuntimeException(
String.format("The file '%s' couldn't be loaded.", LOGGING_PROPERTIES_FILE));
}

} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
return Logger.getLogger(Application.class.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import com.sinch.sdk.SinchClient;
import com.sinch.sdk.models.Configuration;
import com.sinch.sdk.models.ConversationRegion;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;
import java.util.logging.Logger;

public class SinchClientHelper {

private static final Logger LOGGER = Logger.getLogger(SinchClientHelper.class.getName());

private static final String SINCH_PROJECT_ID = "SINCH_PROJECT_ID";
private static final String SINCH_KEY_ID = "SINCH_KEY_ID";
private static final String SINCH_KEY_SECRET = "SINCH_KEY_SECRET";

private static final String CONVERSATION_REGION = "CONVERSATION_REGION";

private static final String CONFIG_FILE = "config.properties";

public static SinchClient initSinchClient() {

LOGGER.info("Initializing client");

Configuration configuration = getConfiguration();

return new SinchClient(configuration);
}

private static Configuration getConfiguration() {

Properties properties = loadProperties();

Configuration.Builder builder = Configuration.builder();

manageUnifiedCredentials(properties, builder);
manageConversationConfiguration(properties, builder);

return builder.build();
}

private static Properties loadProperties() {

Properties properties = new Properties();

try (InputStream input =
SinchClientHelper.class.getClassLoader().getResourceAsStream(CONFIG_FILE)) {
if (input != null) {
properties.load(input);
} else {
LOGGER.severe(String.format("'%s' file could not be loaded", CONFIG_FILE));
}
} catch (IOException e) {
LOGGER.severe(String.format("Error loading properties from '%s'", CONFIG_FILE));
}

return properties;
}

static void manageUnifiedCredentials(Properties properties, Configuration.Builder builder) {

Optional<String> projectId = getConfigValue(properties, SINCH_PROJECT_ID);
Optional<String> keyId = getConfigValue(properties, SINCH_KEY_ID);
Optional<String> keySecret = getConfigValue(properties, SINCH_KEY_SECRET);

projectId.ifPresent(builder::setProjectId);
keyId.ifPresent(builder::setKeyId);
keySecret.ifPresent(builder::setKeySecret);
}

private static void manageConversationConfiguration(
Properties properties, Configuration.Builder builder) {

Optional<String> region = getConfigValue(properties, CONVERSATION_REGION);

region.ifPresent(value -> builder.setConversationRegion(ConversationRegion.from(value)));
}

private static Optional<String> getConfigValue(Properties properties, String key) {
String value = null != System.getenv(key) ? System.getenv(key) : properties.getProperty(key);

// empty value means setting not set
if (null != value && value.trim().isEmpty()) {
return Optional.empty();
}

return Optional.ofNullable(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package conversation;

import com.sinch.sdk.domains.conversation.api.v1.ConversationService;

public class ConversationQuickStart {

private final ConversationService conversationService;

public ConversationQuickStart(ConversationService conversationService) {
this.conversationService = conversationService;

// replace by your code and business logic
Snippet.execute(this.conversationService);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package conversation;

import com.sinch.sdk.domains.conversation.api.v1.ConversationService;
import com.sinch.sdk.domains.conversation.api.v1.MessagesService;
import com.sinch.sdk.domains.conversation.models.v1.*;
import com.sinch.sdk.domains.conversation.models.v1.messages.*;
import com.sinch.sdk.domains.conversation.models.v1.messages.request.*;
import com.sinch.sdk.domains.conversation.models.v1.messages.response.SendMessageResponse;
import com.sinch.sdk.domains.conversation.models.v1.messages.types.text.*;
import java.util.*;
import java.util.Collections;
import java.util.logging.Logger;

public class Snippet {

private static final Logger LOGGER = Logger.getLogger(Snippet.class.getName());

static void execute(ConversationService conversationService) {

MessagesService messagesService = conversationService.messages();

String appId = "CONVERSATION_APPLICATION_ID";
String from = "SINCH_VIRTUAL_PHONE_NUMBER";
String to = "RECIPIENT_PHONE_NUMBER";

String body = "This is a test Conversation message using the Sinch Java SDK.";

ChannelRecipientIdentities recipients =
ChannelRecipientIdentities.of(
ChannelRecipientIdentity.builder()
.setChannel(ConversationChannel.SMS)
.setIdentity(to)
.build());

AppMessage<TextMessage> message =
AppMessage.<TextMessage>builder()
.setBody(TextMessage.builder().setText(body).build())
.build();

SendMessageRequest<TextMessage> request =
SendMessageRequest.<TextMessage>builder()
.setAppId(appId)
.setRecipient(recipients)
.setMessage(message)
.setChannelProperties(Collections.singletonMap("SMS_SENDER", from))
.build();

LOGGER.info("Sending SMS Text using Conversation API");

SendMessageResponse value = messagesService.sendMessage(request);

LOGGER.info("Response: " + value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Required credentials for using the Conversation API
SINCH_PROJECT_ID=
SINCH_KEY_ID=
SINCH_KEY_SECRET=

# See https://github.com/sinch/sinch-sdk-java/blob/main/client/src/main/com/sinch/sdk/models/ConversationRegion.java for list of supported values
#CONVERSATION_REGION = us
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = INFO
com.sinch.level = INFO

java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%4$-7s %2$s] %5$s %n

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Sinch Getting started

Code is related to [Rent and configure your virtual number using Java](https://developers.sinch.com/docs/numbers/getting-started).

See [Client template README](../../../client/README.md) for details about configuration and usage.
Loading
Loading