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.twitter.internal.action;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.twitter.internal.TwitterHandler;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.ActionOutput;
20 import org.openhab.core.automation.annotation.RuleAction;
21 import org.openhab.core.thing.binding.ThingActions;
22 import org.openhab.core.thing.binding.ThingActionsScope;
23 import org.openhab.core.thing.binding.ThingHandler;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * The {@link TweetActions} class defines rule actions for sending tweet
30 * @author Scott Hanson - Initial contribution
32 @ThingActionsScope(name = "twitter")
34 public class TwitterActions implements ThingActions {
36 private final Logger logger = LoggerFactory.getLogger(TwitterActions.class);
38 private @Nullable TwitterHandler handler;
40 @RuleAction(label = "@text/sendTweetActionLabel", description = "@text/sendTweetActionDescription")
41 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendTweet(
42 @ActionInput(name = "text") @Nullable String text) {
44 logger.warn("Cannot send Tweet as text is missing.");
48 final TwitterHandler handler = this.handler;
49 if (handler == null) {
50 logger.debug("Handler is null, cannot tweet.");
53 return handler.sendTweet(text);
57 @RuleAction(label = "@text/sendAttachmentTweetActionLabel", description = "@text/sendAttachmentTweetActionDescription")
58 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendTweetWithAttachment(
59 @ActionInput(name = "text") @Nullable String text, @ActionInput(name = "url") @Nullable String urlString) {
61 logger.warn("Cannot send Tweet as text is missing.");
64 if (urlString == null) {
65 logger.warn("Cannot send Tweet as urlString is missing.");
69 final TwitterHandler handler = this.handler;
70 if (handler == null) {
71 logger.debug("Handler is null, cannot tweet.");
74 return handler.sendTweet(text, urlString);
78 @RuleAction(label = "@text/sendDirectMessageActionLabel", description = "@text/sendDirectMessageActionDescription")
79 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendDirectMessage(
80 @ActionInput(name = "recipient") @Nullable String recipient,
81 @ActionInput(name = "text") @Nullable String text) {
82 if (recipient == null) {
83 logger.warn("Cannot send Direct Message as recipient is missing.");
87 logger.warn("Cannot send Direct Message as text is missing.");
91 final TwitterHandler handler = this.handler;
92 if (handler == null) {
93 logger.debug("Handler is null, cannot tweet.");
96 return handler.sendDirectMessage(recipient, text);
100 public static boolean sendTweet(ThingActions actions, @Nullable String text) {
101 return ((TwitterActions) actions).sendTweet(text);
104 public static boolean sendTweetWithAttachment(ThingActions actions, @Nullable String text,
105 @Nullable String urlString) {
106 return ((TwitterActions) actions).sendTweetWithAttachment(text, urlString);
109 public static boolean sendDirectMessage(ThingActions actions, @Nullable String recipient, @Nullable String text) {
110 return ((TwitterActions) actions).sendDirectMessage(recipient, text);
114 public void setThingHandler(@Nullable ThingHandler handler) {
115 if (handler instanceof TwitterHandler) {
116 this.handler = (TwitterHandler) handler;
121 public @Nullable ThingHandler getThingHandler() {