Skip to content

Commit

Permalink
0.0.2
Browse files Browse the repository at this point in the history
Action added - ask imsg Question, if confimed then runs the specified action group
Only one question, per buddy at a time
[can have multiple buddy questions though]
  • Loading branch information
ghawken committed Oct 29, 2018
1 parent ad7a497 commit 55a1e57
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 6 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,21 @@ Alarm on

## Two current actions

### Send Imsg and send iMsgFile
### Send Imsg

Allow to send imsg text to any buddy you know
Tick box - to send reply to last buddy received message from enabling ongoing discussion
Allows variable and device state substitution

send iMsgFile
### Send iMsgFile
- sends file/image/animated gif to buddy
(again has tickbox for last buddy)

### Ask iMsg QUestion
Sends question to Buddy - waits the timeout period for response
If timeout sends a timeout reply
If confirmation received - then runs the specified action group
Send specified reply (%%V:112123%%) substitution allowed

#todo

Expand Down
2 changes: 1 addition & 1 deletion iMessage.indigoPlugin/Contents/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>PluginVersion</key>
<string>0.0.1</string>
<string>0.0.2</string>
<key>ServerApiVersion</key>
<string>2.0.0</string>
<key>IwsApiVersion</key>
Expand Down
34 changes: 33 additions & 1 deletion iMessage.indigoPlugin/Contents/Server Plugin/Actions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,38 @@
</Field>
</ConfigUI>
</Action>

<Action id="sendQuestion">
<Name>Send iMsg Question</Name>
<CallbackMethod>sendiMsgQuestion</CallbackMethod>
<ConfigUI>
<Field type="label" id="schedule_label">
<Label>Select the Buddy Handle to send message to</Label>
</Field>
<Field type="textfield" id="buddyId">
<Label>Buddy Handle:</Label>
</Field>
<Field type="checkbox" id="lastBuddy">
<Label>Use last Buddy Handle message received from?:</Label>
</Field>
<Field type="textfield" id="message">
<Label>Message to Send:</Label>
</Field>
<Field type="label" id="timeout_label">
<Label>Select the Timeout to wait for reply in Seconds</Label>
</Field>
<Field type="textfield" id="timeout">
<Label>Timeout (seconds)</Label>
</Field>
<Field type="menu" id="actiongroup">
<Label>Action Group to Run:</Label>
<List class="indigo.actionGroups" filter=""/>
</Field>

<Field type="textfield" id="confirmedimsg">
<Label>Confirmation imsg to send:</Label>
</Field>

</ConfigUI>
</Action>
</Actions>

116 changes: 114 additions & 2 deletions iMessage.indigoPlugin/Contents/Server Plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def __init__(self, pluginId, pluginDisplayName, pluginVersion, pluginPrefs):

self.lastcommand = ()
self.lastBuddy =''
self.awaitingConfirmation = [] # buddy handle within here if waiting a reply yes or no
# 'buddy', 'AGtorun', 'timestamp', 'iMsgConfirmed'

self.allowedBuddies = self.pluginPrefs.get('allowedBuddies','')
self.prefServerTimeout = int(self.pluginPrefs.get('configMenuServerTimeout', "15"))
Expand Down Expand Up @@ -151,6 +153,8 @@ def runConcurrentThread(self):
messages = self.sql_fetchmessages()
if len(messages)>0:
self.parsemessages(messages)
if len(self.awaitingConfirmation)>0:
self.checkTimeout()
if x>4:
x=0
self.lastcommand = ''
Expand Down Expand Up @@ -259,6 +263,21 @@ def as_sendpicture(self, imsgUser, imsgFile):
# Parse Messages
########

def checkTimeout(self):
if self.debugextra:
self.debugLog(u"checkTimeout method called.")
for sublist in self.awaitingConfirmation:
if t.time() > int(sublist[2]):
self.logger.debug(u'Timeout for '+unicode(sublist)+' occured. Removing and sending timeout msg')
self.as_sendmessage(sublist[0],'Timeout waiting for reply')
self.awaitingConfirmation = [ subl for subl in self.awaitingConfirmation if subl[0]!=sublist[0] ]
if self.debugextra:
self.logger.debug(u'self.awaitingConfirmation modified now equals:' + unicode(self.awaitingConfirmation))
#make new nested list removing the most recent buddy handle
# 'buddy', 'AGtorun', 'timestamp', 'iMsgConfirmed'
return


def parsemessages(self, messages):
if self.debugextra:
self.debugLog(u"parse messages() method called.")
Expand Down Expand Up @@ -286,10 +305,55 @@ def parsemessages(self, messages):
if self.debugextra:
self.logger.debug( u'self.lastcommand : '+ unicode(self.lastcommand )+ ' & message =:' + unicode(messages))

self.triggerCheck('', 'commandReceived', messages[1] )
self.lastcommand = messages
self.lastBuddy = messages[0]

for sublist in self.awaitingConfirmation:
if sublist[0] == messages[0]:
# Buddle has a outstanding confirmation awaited.
# check against valid replies
if self.checkanswer(messages[0],messages[1],sublist):
if self.debugextra:
self.logger.debug(u'Confirmation received so parsing message ending. No trigger check.')

return

self.triggerCheck('', 'commandReceived', messages[1] )

#######
#
def checkanswer(self, buddyHandle, message, sublist):
if self.debugextra:
self.debugLog(u"checkanswer() method called.")

valid = {"yes": True, "y": True, "ye": True, 'yeah':True, 'ok':True, "no": False, "n": False, 'nope':False, 'never':False}

if message.lower() in valid:
if valid[message.lower()]:
# if True run actions
if self.debugextra:
self.debugLog(u"checkanswer() Valid Reply Found Calling Action Group and sending reply.")
indigo.actionGroup.execute(int(sublist[1]))
self.logger.info(u'iMsg: Postive Answer Received. Running action group and sending reply.')
self.as_sendmessage(sublist[0],sublist[3])
self.awaitingConfirmation = [ subl for subl in self.awaitingConfirmation if subl[0]!=buddyHandle ]
if self.debugextra:
self.logger.debug(u'self.awaitingConfirmation now equals:' + unicode(self.awaitingConfirmation))
#make new nested list removing the most recent buddy handle
# 'buddy', 'AGtorun', 'timestamp', 'iMsgConfirmed'
return True
else:
if self.debugextra:
self.debugLog(u"checkanswer() False Reply Found.")
self.logger.info(u'iMsg: Negative Answer Received. No action taken.')
self.as_sendmessage(sublist[0], 'Ok. No action Taken.')
self.awaitingConfirmation = [subl for subl in self.awaitingConfirmation if subl[0] != buddyHandle]
if self.debugextra:
self.logger.debug(u'self.awaitingConfirmation now equals:' + unicode(self.awaitingConfirmation))
return True

return False

#######

def shutdown(self):
Expand Down Expand Up @@ -401,6 +465,52 @@ def stopSleep(self, start_sleep):
##########
# Action Groups
##########
def sendiMsgQuestion(self, action):
if self.debugextra:
self.debugLog(u"sendImsgQuestion() method called.")
theMessage = self.substitute(action.props.get("message", ""))
buddyHandle = action.props.get('buddyId','')
lastbuddy = action.props.get('lastBuddy', False)
timeout = action.props.get('timeout',120)
confirmationmsg = self.substitute(action.props.get('confirmedimsg',''))
AGtoRun = action.props.get('actiongroup','')
expiredtime = t.time() + int(timeout)

if lastbuddy:
buddyHandle = str(self.lastBuddy)

if self.debugextra:
self.debugLog(u"sendImsgQuestion() buddyHandle:" + unicode(buddyHandle) + u' and theMessage:' + unicode(
theMessage) + u' and use lastBuddy:' + unicode(lastbuddy))
if buddyHandle == '':
self.logger.debug(u'Message sending aborted as buddyHandle is blank')
self.logger.debug(u'If using LastBuddy need to send message before this is filled')
return


try:
# 'buddy', 'AGtorun', 'timestamp', 'iMsgConfirmed'
add = [ buddyHandle, AGtoRun, expiredtime, confirmationmsg ]
buddyalreadywaitingconfirmation = False
for sublist in self.awaitingConfirmation:
if sublist[0] == buddyHandle:
self.logger.debug(u'buddyhandle already awaiting confirmation - End. Dont ask new Question')
if self.debugextra:
self.logger.debug(u'self.awaitingConfirmation equals:' + unicode(self.awaitingConfirmation))
buddyalreadywaitingconfirmation= True
return #

self.as_sendmessage(buddyHandle, theMessage)
if buddyalreadywaitingconfirmation == False: # check not already waiting - can ask two questions same time..
self.awaitingConfirmation.append(add) # if not in there add
if self.debugextra:
self.logger.debug(u'self.awaitingConfirmation now equals:'+unicode(self.awaitingConfirmation))


except:
self.logger.exception(u'Exception in SendImsgQuestion')
return


def sendiMsg(self, action):
if self.debugextra:
Expand Down Expand Up @@ -493,7 +603,7 @@ def triggerCheck(self, device, triggertype, imsgcmdreceived):
try:
for triggerId, trigger in sorted(self.triggers.iteritems()):
if self.debugtriggers:
self.logger.debug("Checking Trigger %s (%s), Type: %s, and event : %s" % (trigger.name, trigger.id, trigger.pluginTypeId, triggertype))
self.logger.debug("Checking Trigger: %s (%s), Type: %s, and event : %s" % (trigger.name, trigger.id, trigger.pluginTypeId, triggertype))
#self.logger.error(unicode(trigger))
if trigger.pluginTypeId == "commandReceived" and triggertype =='commandReceived':
if self.debugtriggers:
Expand All @@ -502,6 +612,8 @@ def triggerCheck(self, device, triggertype, imsgcmdreceived):
if self.debugtriggers:
self.logger.debug("===== Executing commandReceived Trigger %s (%d)" % (trigger.name, trigger.id))
indigo.trigger.execute(trigger)


except:
self.logger.exception(u'Exception within Trigger Check')
return
Expand Down

0 comments on commit 55a1e57

Please sign in to comment.