Changing remote branch to PLRG Git server.
[smartapps.git] / official / smartblock-chat-sender.groovy
1 /**
2  *  Copyright 2015 SmartThings
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  *  in compliance with the License. You may obtain a copy of the License at:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11  *  for the specific language governing permissions and limitations under the License.
12  *
13  *  SmartClockChatSender
14  *
15  *  Author: Steve Vlaminck
16  *
17  *  Date: 2014-02-03
18  */
19
20 definition(
21     name: "SmartBlock Chat Sender",
22     namespace: "vlaminck/Minecraft",
23     author: "SmartThings",
24     description: "Send chat messages into Minecraft via the SmartBlock mod",
25     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
26     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
27 )
28
29 preferences {
30         page(name: "chatPage", title: "Send Notifications Into Minecraft", install: true, uninstall: true) {
31                 section {
32                         input(name: "chatsEnabled", type: "bool", title: "Enable This Notification?", defaultValue: "true")
33                         input(name: "modes", type: "mode", title: "Notify SmartBlock users when the mode changes to:", description: "(optional)", multiple: true, required: false)
34                         input(name: "username", type: "string", title: "Only send to this username", required: false, description: "(optional)")
35                         input(name: "customMessage", type: "string", title: "Custom message?", required: false, description: "(optional)")
36                 }
37                 section(hidden: true, hideable: true, title: "Other Options") {
38                         label(title: "Label this Notification", required: false)
39                 }
40         }
41 }
42
43 def installed() {
44         log.debug "Installed with settings: ${settings}"
45
46         initialize()
47 }
48
49 def updated() {
50         log.debug "Updated with settings: ${settings}"
51
52         unsubscribe()
53         initialize()
54 }
55
56 def initialize() {
57         subscribe(location, modeChangeHandler)
58 }
59
60 def modeChangeHandler(evt) {
61         def newMode = evt.value
62         log.debug "evt: ${newMode}"
63         if (modes && modes.contains(newMode))
64         {
65                 def message = customMessage ?: "SmartThings mode has changed to: \"${newMode}\""
66                 chatMessageToMC(message)
67         }
68 }
69
70 def chatMessageToMC(message) {
71
72         def parent = app.getParent()
73
74         def url = "${parent.getServerURL()}/chat?message=${message.encodeAsURL()}"
75
76         if (username)
77         {
78                 url += "&username=${username.encodeAsURL()}"
79         }
80         log.debug "POST to ${url}"
81
82         httpPost(url, "foo=bar") { response ->
83                 content = response.data
84                 log.debug "response: ${content}"
85         }
86
87 }