2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.amazonechocontrol.internal.channelhandler;
15 import java.io.IOException;
16 import java.net.URISyntaxException;
17 import java.time.LocalDateTime;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.amazonechocontrol.internal.Connection;
22 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDevices.Device;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.types.Command;
26 import com.google.gson.Gson;
29 * The {@link ChannelHandlerSendMessage} is responsible for the announcement
32 * @author Michael Geramb - Initial contribution
35 public class ChannelHandlerSendMessage extends ChannelHandler {
37 private static final String CHANNEL_NAME = "sendMessage";
38 private @Nullable AccountJson accountJson;
39 private int lastMessageId = 1000;
41 public ChannelHandlerSendMessage(IAmazonThingHandler thingHandler, Gson gson) {
42 super(thingHandler, gson);
46 public boolean tryHandleCommand(Device device, Connection connection, String channelId, Command command)
47 throws IOException, URISyntaxException, InterruptedException {
48 if (channelId.equals(CHANNEL_NAME)) {
49 if (command instanceof StringType) {
50 String commandValue = ((StringType) command).toFullString();
51 String baseUrl = "https://alexa-comms-mobile-service." + connection.getAmazonSite();
53 AccountJson currentAccountJson = this.accountJson;
54 if (currentAccountJson == null) {
55 String accountResult = connection.makeRequestAndReturnString(baseUrl + "/accounts");
56 AccountJson @Nullable [] accountsJson = gson.fromJson(accountResult, AccountJson[].class);
57 if (accountsJson == null) {
60 for (AccountJson accountJson : accountsJson) {
61 Boolean signedInUser = accountJson.signedInUser;
62 if (signedInUser != null && signedInUser) {
63 this.accountJson = accountJson;
64 currentAccountJson = accountJson;
69 if (currentAccountJson == null) {
72 String commsId = currentAccountJson.commsId;
73 if (commsId == null) {
76 String senderCommsId = commsId;
77 String receiverCommsId = commsId;
79 SendConversationJson conversationJson = new SendConversationJson();
80 conversationJson.conversationId = "amzn1.comms.messaging.id.conversationV2~31e6fe8f-8b0c-4e84-a1e4-80030a09009b";
81 conversationJson.clientMessageId = java.util.UUID.randomUUID().toString();
82 conversationJson.messageId = lastMessageId++;
83 conversationJson.sender = senderCommsId;
84 conversationJson.time = LocalDateTime.now().toString();
85 conversationJson.payload.text = commandValue;
87 String sendConversationBody = this.gson.toJson(new SendConversationJson[] { conversationJson });
88 String sendUrl = baseUrl + "/users/" + senderCommsId + "/conversations/" + receiverCommsId
90 connection.makeRequestAndReturnString("POST", sendUrl, sendConversationBody, true, null);
97 private void refreshChannel() {
98 thingHandler.updateChannelState(CHANNEL_NAME, new StringType(""));
101 @SuppressWarnings("unused")
102 private static class AccountJson {
103 public @Nullable String commsId;
104 public @Nullable String directedId;
105 public @Nullable String phoneCountryCode;
106 public @Nullable String phoneNumber;
107 public @Nullable String firstName;
108 public @Nullable String lastName;
109 public @Nullable String phoneticFirstName;
110 public @Nullable String phoneticLastName;
111 public @Nullable String commsProvisionStatus;
112 public @Nullable Boolean isChild;
113 public @Nullable Boolean signedInUser;
114 public @Nullable Boolean commsProvisioned;
115 public @Nullable Boolean speakerProvisioned;
118 @SuppressWarnings("unused")
119 private static class SendConversationJson {
120 public @Nullable String conversationId;
121 public @Nullable String clientMessageId;
122 public @Nullable Integer messageId;
123 public @Nullable String time;
124 public @Nullable String sender;
125 public String type = "message/text";
126 public Payload payload = new Payload();
127 public Integer status = 1;
129 private static class Payload {
130 public @Nullable String text;