Skip to content

feat: New snippets format (Numbers) and launcher #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 30, 2025
Merged
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
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,64 @@
Sinch Java SDK Code Snippets Repository

This repository contains code snippets related to [Sinch JAVA SDK](https://github.com/sinch/sinch-sdk-java)

Snippets can be used as starting point to support Sinch products from your own application.

## Requirements
- JDK 8 or later
- [Sinch account](https://dashboard.sinch.com)

## Snippet execution
Launcher helpers are provided to execute snippets and minimize time to setup your first Java application based onto Sinch SDK

### Snippets execution settings
When executing a snippet you will need to provide certain information about your Sinch account (credentials, Sinch virtual phone number, ...)

This settings can be placed directly in the snippet source or you can choose to edit the [configuration file](./snippets/src/main/resources/config.properties), in which case the settings will be shared and used automatically by each snippet.

### Linux platform
Launch script is [here](./snippets/launcher)

Execution:
```shell
cd snippets
launcher <SNIPPET_SOURCE_PATH>
```
Where `SNIPPET_SOURCE_PATH` is path to snippet source (see [Available Snippets](#available-snippets) for available snippets)

e.g.:
```shell
launcher numbers/SearchForAvailableNumbers
...
launcher regions/List

```

## Available Snippets

- Numbers
- [numbers/Rent](snippets/src/main/java/numbers/Rent.java)
- [numbers/RentAny](snippets/src/main/java/numbers/RentAny.java)
- [numbers/SearchForAvailableNumbers](snippets/src/main/java/numbers/SearchForAvailableNumbers.java)
- [numbers/CheckAvailability](snippets/src/main/java/numbers/CheckAvailability.java)
- [numbers/List](snippets/src/main/java/numbers/List.java)
- [numbers/Update](snippets/src/main/java/numbers/Update.java)
- [numbers/Get](snippets/src/main/java/numbers/Get.java)
- [numbers/Release](snippets/src/main/java/numbers/Release.java)
- Emergency Addresses
- [numbers/ValidateEmergencyAddress](snippets/src/main/java/numbers/ValidateEmergencyAddress.java)
- [numbers/ProvisionEmergencyAddress](snippets/src/main/java/numbers/ProvisionEmergencyAddress.java)
- [numbers/GetEmergencyAddress](snippets/src/main/java/numbers/GetEmergencyAddress.java)
- [numbers/DeprovisionEmergencyAddress](snippets/src/main/java/numbers/DeprovisionEmergencyAddress.java)
- Regions
- [numbers/regions/List](snippets/src/main/java/numbers/regions/List.java)
- Callbacks
- [numbers/callback/Get](snippets/src/main/java/numbers/callback/Get.java)
- [numbers/callback/Update](snippets/src/main/java/numbers/callback/Update.java)







19 changes: 19 additions & 0 deletions snippets/.mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
9 changes: 1 addition & 8 deletions snippets/compile.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
#!/bin/sh

SNIPPETS=`find . -type f -regex '.*Snippet\.java$'`

echo "Compiling snippets:"
for snippet in $SNIPPETS
do
echo " - Snippet: $snippet"
mvn compile -Dsnippet="$(dirname $snippet)" || exit 1
done
./mvnw clean compile
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
package conversation;
package conversation.messages;

import com.sinch.sdk.domains.conversation.api.v1.ConversationService;
import com.sinch.sdk.SinchClient;
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 com.sinch.sdk.models.Configuration;
import java.util.*;
import java.util.Collections;
import java.util.logging.*;
import java.util.logging.Logger;

public class Snippet {
public class SendSMS {

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

static void execute(ConversationService conversationService) {
public static void main(String[] args) {

MessagesService messagesService = conversationService.messages();
String projectId = "SINCH_PROJECT_ID";
String keyId = "SINCH_KEY_ID";
String keySecret = "SINCH_KEY_SECRET";

Configuration configuration =
Configuration.builder()
.setProjectId(projectId)
.setKeyId(keyId)
.setKeySecret(keySecret)
.build();

SinchClient client = new SinchClient(configuration);

MessagesService service = client.conversation().v1().messages();

String appId = "YOUR_app_id";
String from = "YOUR_sms_sender";
Expand Down Expand Up @@ -48,7 +63,7 @@ static void execute(ConversationService conversationService) {

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

SendMessageResponse value = messagesService.sendMessage(request);
SendMessageResponse value = service.sendMessage(request);

LOGGER.info("Response: " + value);
}
Expand Down
20 changes: 20 additions & 0 deletions snippets/launcher
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

SNIPPET=$1

echo "Running \"${SNIPPET}\" snippet"

# change
# directory separator by package separator
# supress '.java' suffix if any
CLSS=$(echo "${SNIPPET}" | sed 's/\//./g;s/\.java$//g')

./mvnw \
-q \
clean \
package \
exec:java \
-Djava.util.logging.config.file=src/main/resources/logging.properties \
-Dexec.mainClass="$CLSS" \
|| exit 1

Loading
Loading