This repository has been archived by the owner on Apr 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_line.py
74 lines (63 loc) · 2.31 KB
/
command_line.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
import subprocess
from yowsup.layers.interface import YowInterfaceLayer, ProtocolEntityCallback
from yowsup.layers.protocol_messages.protocolentities import TextMessageProtocolEntity
import threading
import logging
logger = logging.getLogger(__name__)
class QueryLayer(YowInterfaceLayer):
def __init__(self):
super(QueryLayer, self).__init__()
self.ackQueue = []
self.lock = threading.Condition()
@ProtocolEntityCallback("message")
def onMessage(self, messageProtocolEntity):
self.lock.acquire()
self.toLower(messageProtocolEntity.ack()) # ACKS
self.toLower(messageProtocolEntity.ack(True))
numeros = ['Your number']
msg = messageProtocolEntity.getBody() # MENSAJE
if messageProtocolEntity.getType() == 'text' and numeros[0] in messageProtocolEntity.getFrom():
query = self.onTextMessage(messageProtocolEntity)
if query == 'Ok':
self.Respuesta('Ok',messageProtocolEntity.getFrom())
elif query:
self.Respuesta(query,messageProtocolEntity.getFrom())
else:
self.Respuesta('Error',messageProtocolEntity.getFrom())
else:
print messageProtocolEntity.getFrom()+': '+msg+'\n'
self.lock.release()
@ProtocolEntityCallback("receipt")
def onReceipt(self, entity):
self.lock.acquire()
self.toLower(entity.ack())
self.lock.release()
def onAck(self, entity):
self.lock.acquire()
if entity.getId() in self.ackQueue:
self.ackQueue.pop(self.ackQueue.index(entity.getId()))
if not len(self.ackQueue):
self.lock.release()
print "Comando enviado\n"
self.lock.release()
def Respuesta(self, query, numero):
self.lock.acquire()
messageEntity = TextMessageProtocolEntity(query, to = numero)
self.ackQueue.append(messageEntity.getId())
self.toLower(messageEntity)
self.onAck(messageEntity)
self.lock.release()
def onTextMessage(self,messageProtocolEntity):
self.lock.acquire()
cmd = messageProtocolEntity.getBody()
print "CMD: %s" %cmd
cmd = cmd.strip().lstrip()
output = ''
try:
output = subprocess.check_output(cmd, shell = True)
if output == '':return 'Ok'
except: return ''
print output.strip()
return output
self.lock.release()