|
| 1 | +# Messages API quickstart |
| 2 | + |
| 3 | +This quick guide aims to help you start with [Infobip Messages API](https://www.infobip.com/docs/api/platform/messages-api/sending-message/send-messages-api-message). After reading it, you should know how to use Messages API, send various types of messages, receive incoming messages, and receive delivery reports. |
| 4 | + |
| 5 | +Messages API supports 11 different channels: SMS, MMS, RCS, WhatsApp, Viber Business Messages, Viber Bots, Apple Messages for Business, Google Business Messages, Instagram Direct Messages, Messenger, and LINE Official Notification. |
| 6 | + |
| 7 | +The first step is to create an `ApiClient` instance with some configuration. |
| 8 | + |
| 9 | +````java |
| 10 | + ApiClient apiClient = ApiClient.forApiKey(ApiKey.from(API_KEY)) |
| 11 | + .withBaseUrl(BaseUrl.from(BASE_URL)) |
| 12 | + .build(); |
| 13 | +```` |
| 14 | + |
| 15 | +With that ready, you can now create an instance of `MessagesApi` which allows you to send messages using Messages API. |
| 16 | + |
| 17 | +````java |
| 18 | + MessagesApi messagesApi = new MessagesApi(apiClient); |
| 19 | +```` |
| 20 | + |
| 21 | +## Activate your test senders |
| 22 | + |
| 23 | +Before sending a message using Messages API, you need to activate your sender(s) and connect to our test domain. |
| 24 | + |
| 25 | +Here you can find the example on how to activate and use **WhatsApp and SMS channels**. |
| 26 | + |
| 27 | +To activate the WhatsApp test sender, add the **447860099299** Infobip sender to your WhatsApp contacts and send a message containing your Infobip account username. |
| 28 | + |
| 29 | +To use the SMS test sender, simply send a message by using **InfoSMS** sender. |
| 30 | + |
| 31 | +You are now ready to send your first message. |
| 32 | + |
| 33 | +**IMPORTANT NOTE:** Keep in mind that for test purposes you can only send messages to a number you registered when you created your Infobip account. |
| 34 | + |
| 35 | +## Send your first message |
| 36 | + |
| 37 | +The easiest way to start with Messages API is to send a text message. First you need to prepare the message you want to send, like on snippet below: |
| 38 | + |
| 39 | +````java |
| 40 | + MessagesApiMessage messagesApiMessage = new MessagesApiMessage() |
| 41 | + .channel(MessagesApiOutboundMessageChannel.SMS) |
| 42 | + .sender("48123234567") |
| 43 | + .destinations( |
| 44 | + List.of( |
| 45 | + new MessagesApiToDestination() |
| 46 | + .to("447491163443") |
| 47 | + ) |
| 48 | + ) |
| 49 | + .content( |
| 50 | + new MessagesApiMessageContent() |
| 51 | + .body( |
| 52 | + new MessagesApiMessageTextBody() |
| 53 | + .text("Sent using Infobip's Java client library!") |
| 54 | + ) |
| 55 | + ); |
| 56 | + |
| 57 | + MessagesApiRequest messagesApiRequest = new MessagesApiRequest() |
| 58 | + .addMessagesItem(messagesApiMessage); |
| 59 | +```` |
| 60 | + |
| 61 | +Send the message invoking the appropriate send method and store the results in a new variable. |
| 62 | + |
| 63 | +````java |
| 64 | + MessagesApiResponse messageInfo = null; |
| 65 | + try { |
| 66 | + messageInfo = messagesApi |
| 67 | + .sendMessagesApiMessage(messagesApiRequest) |
| 68 | + .execute(); |
| 69 | + } catch (ApiException apiException) { |
| 70 | + // HANDLE THE EXCEPTION |
| 71 | + } |
| 72 | + |
| 73 | +```` |
| 74 | + |
| 75 | +Once the invocation finishes, you can inspect the results and print a status description, as shown below. |
| 76 | + |
| 77 | +````java |
| 78 | + System.out.println(messageInfo.getMessages().get(0).getStatus().getDescription()); |
| 79 | +```` |
| 80 | + |
| 81 | +## How to receive messages |
| 82 | + |
| 83 | +To receive messages using Messages API you must set up the webhook. |
| 84 | + |
| 85 | +Basically, that is just an endpoint implemented on your side where you will accept the requests when a new message arrives. That endpoint will be called by the Infobip API whenever we receive an incoming message for your registered sender(s). |
| 86 | + |
| 87 | +```java |
| 88 | + @PostMapping("/incoming-messages") |
| 89 | + public void receiveMessages(HttpServletRequest request) throws IOException { |
| 90 | + MessagesApiIncomingMessage messages = new JSON().deserialize(request.inputStream(), MessagesApiIncomingMessage.class); |
| 91 | + for (MessagesApiInboundEvent messageData : messages.getResults()) { |
| 92 | + switch (messageData.getEvent()) { |
| 93 | + case MO: |
| 94 | + MessagesApiWebhookEvent messagesApiWebhookEvent = ((MessagesApiWebhookEvent) messageData); |
| 95 | + // INSERT YOUR PROCESSING LOGIC HERE |
| 96 | + break; |
| 97 | + case TYPING_STARTED: |
| 98 | + MessagesApiInboundTypingStartedEvent messagesApiInboundTypingStartedEvent = ((MessagesApiInboundTypingStartedEvent) messageData); |
| 99 | + // INSERT YOUR PROCESSING LOGIC HERE |
| 100 | + break; |
| 101 | + case TYPING_STOPPED: |
| 102 | + MessagesApiInboundTypingStoppedEvent messagesApiInboundTypingStoppedEvent = ((MessagesApiInboundTypingStoppedEvent) messageData); |
| 103 | + // INSERT YOUR PROCESSING LOGIC HERE |
| 104 | + break; |
| 105 | + default: |
| 106 | + throw new IllegalArgumentException("Unexpected message type!"); |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +``` |
| 112 | + |
| 113 | +You can find more details about the structure of the message you can expect on your endpoint on [docs page](https://www.infobip.com/docs/api/platform/messages-api/incoming-message/receive-messages-api-incoming-messages). |
| 114 | + |
| 115 | +## How to receive delivery reports |
| 116 | + |
| 117 | +For each message that you send out, you can get a message delivery report in real time. Subscribe for reports by contacting our support team at <[email protected]>. e.g. `https://{yourDomain}/delivery-reports` |
| 118 | + |
| 119 | +```java |
| 120 | + @PostMapping("/delivery-reports") |
| 121 | + public void receiveDeliveryReports(HttpServletRequest request) throws IOException { |
| 122 | + MessagesApiDeliveryReport reports = new JSON().deserialize(request.getInputStream(), MessagesApiDeliveryReport.class); |
| 123 | + for (MessagesApiDeliveryResult result : reports.getResults()) { |
| 124 | + System.out.println(result.getMessageId() + " - " + result.getStatus().getName()); |
| 125 | + } |
| 126 | + } |
| 127 | +``` |
| 128 | + |
| 129 | +## Use adaptationMode to automatically modify message types |
| 130 | + |
| 131 | +Enhance your Messages API requests by using the `adaptationMode` parameter. It allows you to send messages even if they are unsupported by the channel. |
| 132 | + |
| 133 | +When you set adaptationMode to `true,` Messages API automatically adjusts the message to remove any unsupported elements, ensuring successful delivery. |
| 134 | + |
| 135 | +For instance, if you'd like to include an image in your WhatsApp and SMS messages, set adaptationMode to 'true'. Messages API will handle the delivery for WhatsApp as a message containing an image, while for SMS will provide a link to the image. |
| 136 | + |
| 137 | +On the other hand, if you set adaptationMode to 'false' and try to send a message with an unsupported element to a channel, an error will occur. Make sure to choose the right setting based on your specific channel and content requirements. |
0 commit comments