mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 07:02:02 +01:00
[xmpp] Add action for sending a group message (#17938)
* implement group message sending Signed-off-by: akallabeth <akallabeth@posteo.net>
This commit is contained in:
parent
05382279a9
commit
d4cbc5f839
@ -65,6 +65,23 @@ public class XMPPActions implements ThingActions {
|
|||||||
connection.sendMessage(to, text);
|
connection.sendMessage(to, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RuleAction(label = "publish a group message", description = "Publish a group message using XMPP.")
|
||||||
|
public void publishGroupXMPP(@ActionInput(name = "to", label = "To", description = "Send to") @Nullable String to,
|
||||||
|
@ActionInput(name = "text", label = "Text", description = "Message text") @Nullable String text) {
|
||||||
|
XMPPClientHandler clientHandler = handler;
|
||||||
|
if (clientHandler == null) {
|
||||||
|
logger.warn("XMPP ThingHandler is null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
XMPPClient connection = clientHandler.getXMPPClient();
|
||||||
|
if (to == null || text == null) {
|
||||||
|
logger.warn("Skipping XMPP messaging to {} value {}", to, text);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
connection.sendGroupMessage(to, text);
|
||||||
|
}
|
||||||
|
|
||||||
@RuleAction(label = "publish an image by HTTP", description = "Publish an image by HTTP using XMPP.")
|
@RuleAction(label = "publish an image by HTTP", description = "Publish an image by HTTP using XMPP.")
|
||||||
public void publishXMPPImageByHTTP(
|
public void publishXMPPImageByHTTP(
|
||||||
@ActionInput(name = "to", label = "To", description = "Send to") @Nullable String to,
|
@ActionInput(name = "to", label = "To", description = "Send to") @Nullable String to,
|
||||||
@ -87,6 +104,10 @@ public class XMPPActions implements ThingActions {
|
|||||||
((XMPPActions) actions).publishXMPP(to, text);
|
((XMPPActions) actions).publishXMPP(to, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void publishGroupXMPP(ThingActions actions, @Nullable String to, @Nullable String text) {
|
||||||
|
((XMPPActions) actions).publishGroupXMPP(to, text);
|
||||||
|
}
|
||||||
|
|
||||||
public static void publishXMPPImageByHTTP(ThingActions actions, @Nullable String to, @Nullable String filename) {
|
public static void publishXMPPImageByHTTP(ThingActions actions, @Nullable String to, @Nullable String filename) {
|
||||||
((XMPPActions) actions).publishXMPPImageByHTTP(to, filename);
|
((XMPPActions) actions).publishXMPPImageByHTTP(to, filename);
|
||||||
}
|
}
|
||||||
|
@ -37,8 +37,11 @@ import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
|||||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||||
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
|
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
|
||||||
import org.jivesoftware.smackx.httpfileupload.HttpFileUploadManager;
|
import org.jivesoftware.smackx.httpfileupload.HttpFileUploadManager;
|
||||||
|
import org.jivesoftware.smackx.muc.MultiUserChat;
|
||||||
|
import org.jivesoftware.smackx.muc.MultiUserChatManager;
|
||||||
import org.jxmpp.jid.EntityBareJid;
|
import org.jxmpp.jid.EntityBareJid;
|
||||||
import org.jxmpp.jid.impl.JidCreate;
|
import org.jxmpp.jid.impl.JidCreate;
|
||||||
|
import org.jxmpp.jid.parts.Resourcepart;
|
||||||
import org.jxmpp.stringprep.XmppStringprepException;
|
import org.jxmpp.stringprep.XmppStringprepException;
|
||||||
import org.openhab.binding.xmppclient.internal.handler.XMPPClientMessageSubscriber;
|
import org.openhab.binding.xmppclient.internal.handler.XMPPClientMessageSubscriber;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -55,9 +58,11 @@ public class XMPPClient implements IncomingChatMessageListener, ConnectionListen
|
|||||||
private final Logger logger = LoggerFactory.getLogger(XMPPClient.class);
|
private final Logger logger = LoggerFactory.getLogger(XMPPClient.class);
|
||||||
private @Nullable AbstractXMPPConnection connection;
|
private @Nullable AbstractXMPPConnection connection;
|
||||||
private @Nullable ChatManager chatManager;
|
private @Nullable ChatManager chatManager;
|
||||||
|
private @Nullable MultiUserChatManager multiUserChatManager;
|
||||||
private @Nullable HttpFileUploadManager httpFileUploadManager;
|
private @Nullable HttpFileUploadManager httpFileUploadManager;
|
||||||
private Set<XMPPClientMessageSubscriber> subscribers = new HashSet<>();
|
private Set<XMPPClientMessageSubscriber> subscribers = new HashSet<>();
|
||||||
private final XMPPClientEventlistener eventListener;
|
private final XMPPClientEventlistener eventListener;
|
||||||
|
private String nickname = "";
|
||||||
|
|
||||||
public XMPPClient(XMPPClientEventlistener eventListener) {
|
public XMPPClient(XMPPClientEventlistener eventListener) {
|
||||||
this.eventListener = eventListener;
|
this.eventListener = eventListener;
|
||||||
@ -73,7 +78,7 @@ public class XMPPClient implements IncomingChatMessageListener, ConnectionListen
|
|||||||
subscribers.remove(channel);
|
subscribers.remove(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect(String host, Integer port, String login, String domain, String password,
|
public void connect(String host, Integer port, String login, String nick, String domain, String password,
|
||||||
SecurityMode securityMode) throws XMPPClientConfigException, XMPPClientException {
|
SecurityMode securityMode) throws XMPPClientConfigException, XMPPClientException {
|
||||||
disconnect();
|
disconnect();
|
||||||
String serverHost = domain;
|
String serverHost = domain;
|
||||||
@ -81,6 +86,12 @@ public class XMPPClient implements IncomingChatMessageListener, ConnectionListen
|
|||||||
serverHost = host;
|
serverHost = host;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!nick.isBlank()) {
|
||||||
|
nickname = nick;
|
||||||
|
} else {
|
||||||
|
nickname = login;
|
||||||
|
}
|
||||||
|
|
||||||
XMPPTCPConnectionConfiguration config;
|
XMPPTCPConnectionConfiguration config;
|
||||||
try {
|
try {
|
||||||
config = XMPPTCPConnectionConfiguration.builder() //
|
config = XMPPTCPConnectionConfiguration.builder() //
|
||||||
@ -115,6 +126,10 @@ public class XMPPClient implements IncomingChatMessageListener, ConnectionListen
|
|||||||
ChatManager chatManager = ChatManager.getInstanceFor(connection);
|
ChatManager chatManager = ChatManager.getInstanceFor(connection);
|
||||||
chatManager.addIncomingListener(this);
|
chatManager.addIncomingListener(this);
|
||||||
this.chatManager = chatManager;
|
this.chatManager = chatManager;
|
||||||
|
|
||||||
|
MultiUserChatManager multiUserChatManager = MultiUserChatManager.getInstanceFor(connection);
|
||||||
|
multiUserChatManager.setAutoJoinOnReconnect(true);
|
||||||
|
this.multiUserChatManager = multiUserChatManager;
|
||||||
httpFileUploadManager = HttpFileUploadManager.getInstanceFor(connection);
|
httpFileUploadManager = HttpFileUploadManager.getInstanceFor(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,6 +160,33 @@ public class XMPPClient implements IncomingChatMessageListener, ConnectionListen
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sendGroupMessage(String to, String message) {
|
||||||
|
if (connection == null) {
|
||||||
|
eventListener.onErrorEvent("XMPP connection is null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
MultiUserChatManager chatManager = this.multiUserChatManager;
|
||||||
|
if (chatManager == null) {
|
||||||
|
eventListener.onErrorEvent("XMPP chatManager is null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
EntityBareJid jid = JidCreate.entityBareFrom(to);
|
||||||
|
MultiUserChat chat = multiUserChatManager.getMultiUserChat(jid);
|
||||||
|
|
||||||
|
if (!chat.isJoined()) {
|
||||||
|
chat.join(Resourcepart.from(nickname));
|
||||||
|
}
|
||||||
|
|
||||||
|
chat.sendMessage(message);
|
||||||
|
} catch (XmppStringprepException | SmackException.NotConnectedException | InterruptedException e) {
|
||||||
|
logger.warn("XMPP message sending error", e);
|
||||||
|
} catch (SmackException | XMPPException e) {
|
||||||
|
logger.warn("XMPP message group join error", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void sendImageByHTTP(String to, String filename) {
|
public void sendImageByHTTP(String to, String filename) {
|
||||||
if (connection == null) {
|
if (connection == null) {
|
||||||
logger.warn("XMPP connection is null");
|
logger.warn("XMPP connection is null");
|
||||||
|
@ -26,6 +26,7 @@ public class XMPPClientConfiguration {
|
|||||||
public @Nullable String host;
|
public @Nullable String host;
|
||||||
public Integer port = 5222;
|
public Integer port = 5222;
|
||||||
public String username = "";
|
public String username = "";
|
||||||
|
public String nickname = "";
|
||||||
public String password = "";
|
public String password = "";
|
||||||
public String domain = "";
|
public String domain = "";
|
||||||
public String securityMode = SecurityMode.required.toString();
|
public String securityMode = SecurityMode.required.toString();
|
||||||
|
@ -95,8 +95,8 @@ public class XMPPClientHandler extends BaseBridgeHandler implements XMPPClientEv
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
xmppClient.connect(Objects.requireNonNullElse(config.host, ""), config.port, config.username, config.domain,
|
xmppClient.connect(Objects.requireNonNullElse(config.host, ""), config.port, config.username,
|
||||||
config.password, SecurityMode.valueOf(config.securityMode));
|
config.nickname, config.domain, config.password, SecurityMode.valueOf(config.securityMode));
|
||||||
updateStatus(ThingStatus.ONLINE);
|
updateStatus(ThingStatus.ONLINE);
|
||||||
} catch (XMPPClientConfigException e) {
|
} catch (XMPPClientConfigException e) {
|
||||||
logger.debug("XMPP connection error", e);
|
logger.debug("XMPP connection error", e);
|
||||||
|
@ -12,6 +12,11 @@
|
|||||||
<label>Username</label>
|
<label>Username</label>
|
||||||
<description>The XMPP Username (the left side of JID, e.g. user for JID user@example.com)</description>
|
<description>The XMPP Username (the left side of JID, e.g. user for JID user@example.com)</description>
|
||||||
</parameter>
|
</parameter>
|
||||||
|
<parameter name="nickname" type="text" required="false">
|
||||||
|
<label>Nickname</label>
|
||||||
|
<description>The XMPP Nickname to use in multi user chats. (Defaults to Username)</description>
|
||||||
|
<advanced>true</advanced>
|
||||||
|
</parameter>
|
||||||
<parameter name="domain" type="text" required="true">
|
<parameter name="domain" type="text" required="true">
|
||||||
<label>Domain</label>
|
<label>Domain</label>
|
||||||
<description>The XMPP Domain (the right side of JID, e.g. example.com for JID user@example.com)</description>
|
<description>The XMPP Domain (the right side of JID, e.g. example.com for JID user@example.com)</description>
|
||||||
|
Loading…
Reference in New Issue
Block a user