Search in sources :

Example 1 with Message

use of com.ciscospark.Message in project openhab1-addons by openhab.

the class CiscoSpark method sparkMessage.

/**
     * Sends a Message via Cisco Spark
     *
     * @param msgTxt the Message to send
     * @param roomId the Room to which to send
     *
     * @return <code>true</code>, if sending the message has been successful and
     *         <code>false</code> in all other cases.
     */
@ActionDoc(text = "Sends a Message via Cisco Spark", returns = "<code>true</code>, if sending the tweet has been successful and <code>false</code> in all other cases.")
public static boolean sparkMessage(@ParamDoc(name = "msgTxt", text = "the Message to send") String msgTxt, @ParamDoc(name = "roomId", text = "the Room to which to send") String roomId) {
    if (!CiscoSparkActionService.isProperlyConfigured) {
        logger.debug("Cisco Spark is not yet configured > execution aborted!");
        return false;
    }
    // Validate message
    if (msgTxt == null || "".equals(msgTxt)) {
        logger.warn("Message can't be empty");
        return false;
    }
    // Validate room id
    try {
        UUID.fromString(roomId);
    } catch (IllegalArgumentException e) {
        logger.warn("Room id is not a UUID");
        return false;
    }
    try {
        logger.debug("Creating message");
        // Create the message
        Message msg = new Message();
        msg.setRoomId(roomId);
        msg.setMarkdown(msgTxt);
        logger.debug("About to send message");
        // send the Message
        spark.messages().post(msg);
        logger.debug("Successfully sent Message '{}'", msg.getMarkdown());
        return true;
    } catch (SparkException se) {
        logger.warn("Failed to send message.", se);
        return false;
    } catch (Exception e) {
        logger.warn("Failed to send message!", e);
        return false;
    }
}
Also used : Message(com.ciscospark.Message) SparkException(com.ciscospark.SparkException) AddressException(javax.mail.internet.AddressException) SparkException(com.ciscospark.SparkException) ActionDoc(org.openhab.core.scriptengine.action.ActionDoc)

Example 2 with Message

use of com.ciscospark.Message in project openhab1-addons by openhab.

the class CiscoSpark method sparkPerson.

/**
     * Sends a Message to a Person via Cisco Spark
     *
     * @param msgTxt the Message to send
     * @param personEmail the email of the person to which to send
     *
     * @return <code>true</code>, if sending the message has been successful and
     *         <code>false</code> in all other cases.
     */
@ActionDoc(text = "Sends a Message via Cisco Spark", returns = "<code>true</code>, if sending the tweet has been successful and <code>false</code> in all other cases.")
public static boolean sparkPerson(@ParamDoc(name = "msgTxt", text = "the Message to send") String msgTxt, @ParamDoc(name = "personEmail", text = "the email of the person to which to send") String personEmail) {
    if (!CiscoSparkActionService.isProperlyConfigured) {
        logger.warn("Cisco Spark is not yet configured > execution aborted!");
        return false;
    }
    // Validate message
    if (msgTxt == null || "".equals(msgTxt)) {
        logger.warn("Message can't be empty");
        return false;
    }
    // Validate email
    try {
        InternetAddress email = new InternetAddress(personEmail);
        email.validate();
    } catch (AddressException e) {
        logger.warn("Email address is not valid");
        return false;
    }
    try {
        logger.debug("Creating message");
        // Create the message
        Message msg = new Message();
        msg.setToPersonEmail(personEmail);
        msg.setMarkdown(msgTxt);
        logger.debug("About to send message");
        // send the Message
        spark.messages().post(msg);
        logger.debug("Successfully sent Message '{}'", msg.getMarkdown());
        return true;
    } catch (SparkException se) {
        logger.warn("Failed to send message.", se);
        return false;
    } catch (Exception e) {
        logger.warn("Failed to send message!", e);
        return false;
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(com.ciscospark.Message) SparkException(com.ciscospark.SparkException) AddressException(javax.mail.internet.AddressException) AddressException(javax.mail.internet.AddressException) SparkException(com.ciscospark.SparkException) ActionDoc(org.openhab.core.scriptengine.action.ActionDoc)

Aggregations

Message (com.ciscospark.Message)2 SparkException (com.ciscospark.SparkException)2 AddressException (javax.mail.internet.AddressException)2 ActionDoc (org.openhab.core.scriptengine.action.ActionDoc)2 InternetAddress (javax.mail.internet.InternetAddress)1