def receive_message(credentials): layers = ( EchoLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowPresenceProtocolLayer) ) + YOWSUP_CORE_LAYERS stack = YowStack(layers) # Setting credentials stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, credentials) # WhatsApp server address stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) stack.setProp(EchoLayer.PROP_CREDENTIALS, credentials) # Sending connecting signal stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) try: # Program main loop stack.loop() except AuthError as e: print('Authentication error %s' % e.message) sys.exit(1)
def setUp(self): # YowAuthenticatorLayer.__init__(self) super(YowAuthenticationProtocolLayer, self).__init__() dummyStack = YowStack() self.setStack(dummyStack) self.credentials = ("dummyusername", bytearray("password", "latin-1")) dummyStack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, self.credentials)
class YowsupMiddleManStack(object): def __init__(self, credentials, groups, encryptionEnabled=False): if encryptionEnabled: from yowsup.layers.axolotl import YowAxolotlLayer layers = ( MiddleManLayer, ( YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer, YowIqProtocolLayer, YowCallsProtocolLayer, ), YowAxolotlLayer, YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer, ) else: layers = ( MiddleManLayer, ( YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer, YowIqProtocolLayer, YowCallsProtocolLayer, ), YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer, ) self.stack = YowStack(layers) self.stack.setProp(MiddleManLayer.PROP_GROUPS, groups[0]) self.stack.setCredentials(credentials) def start(self): self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) try: self.stack.loop() except AuthError as e: print("Authentication Error: %s" % e.message)
class YowsupReceiveStack(object): def __init__(self, credentials): layers = (ReceiveLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer), YowAxolotlLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer) self.stack = YowStack(layers) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, credentials) self.stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) self.stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) self.stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) def start(self): #print '-------------------------' #print 'En el start de YowsupReceiveStack' #print '-------------------------' self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) try: self.stack.loop(count=4) #self.stack.loop(timeout = 0.5, discrete = 0.5) except AuthError as e: print("Authentication Error: %s" % e.message)
class YowsupMyStack(object): def __init__(self, credentials): env.CURRENT_ENV = env.S40YowsupEnv() layers = ( MailLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer, YowIqProtocolLayer, YowPresenceProtocolLayer) ) + YOWSUP_CORE_LAYERS self.stack = YowStack(layers) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, credentials) self.stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) self.stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) self.stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) def start(self): self.stack.broadcastEvent( YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) try: self.stack.loop() except AuthError as e: print("Authentication Error: %s" % e.message)
def run(phone, password, config): # @TODO check phone, password is set credentials = (phone.encode(), password.encode()) layers = ( secretary(config), (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer), YowAxolotlLayer, ) + YOWSUP_CORE_LAYERS stack = YowStack(layers) # Setting credentials stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, credentials) # Whatsapp server address stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) # Info about us as WhatsApp client stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) # Sending the connect signal stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) stack.loop()
class YowsupRobotStack(object): def __init__(self, credentials, encryptionEnabled = False,interactionProtocol=None,url=None,flaskApp=None): if encryptionEnabled: from yowsup.layers.axolotl import YowAxolotlLayer layers = ( RobotLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer, YowIqProtocolLayer, YowCallsProtocolLayer), YowAxolotlLayer, YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) else: layers = ( RobotLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer, YowIqProtocolLayer, YowCallsProtocolLayer), YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) self.stack = YowStack(layers) self.stack.setCredentials(credentials) self.stack.setProp('url',url) self.stack.setProp('flaskApp',flaskApp) def start(self): self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) def startThread(): try: self.stack.loop(timeout = 0.5, discrete = 0.5) except AuthError as e: print("Auth Error, reason %s" % e) except KeyboardInterrupt: print("\nYowsdown KeyboardInterrupt") sys.exit(0) except Exception as e: print("\nYowsdown General Exception") print e t1=threading.Thread(target=startThread) t1.daemon=True t1.start()
class YowsupSendStack(object): def __init__(self, credentials, messages, encryptionEnabled = False): """ :param credentials: :param messages: list of (jid, message) tuples :param encryptionEnabled: :return: """ if encryptionEnabled: from yowsup.layers.axolotl import YowAxolotlLayer layers = ( SendLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer), YowAxolotlLayer, YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) else: layers = ( SendLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer), YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) self.stack = YowStack(layers) self.stack.setProp(SendLayer.PROP_MESSAGES, messages) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_PASSIVE, True) self.stack.setCredentials(credentials) def start(self): self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) try: self.stack.loop() except KeyboardInterrupt as e: raise e except AuthError as e: print("Authentication Error: %s" % e.message)
class YowsupSyncStack(object): def __init__(self, credentials, contacts, encryptionEnabled=False): """ :param credentials: :param contacts: list of [jid ] :param encryptionEnabled: :return: """ if encryptionEnabled: from yowsup.layers.axolotl import YowAxolotlLayer layers = ( SyncLayer, (YowAuthenticationProtocolLayer, YowContactsIqProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer), YowAxolotlLayer, YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) else: layers = ( SyncLayer, (YowAuthenticationProtocolLayer, YowContactsIqProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer), YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) self.stack = YowStack(layers) self.stack.setProp(SyncLayer.PROP_CONTACTS, contacts) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_PASSIVE, True) self.stack.setCredentials(credentials) def start(self): self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) try: self.stack.loop() except AuthError as e: print("Authentication Error: {0}".format(e))
def start_server(): layers = ( EchoLayer, YowParallelLayer([YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer]),YowAxolotlLayer ) + YOWSUP_CORE_LAYERS stack = YowStack(layers) stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, CREDENTIALS) #setting credentials stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) #whatsapp server address stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) stack.setProp(YowCoderLayer.PROP_RESOURCE, YowsupEnv.getCurrent().getResource()) #info about us as WhatsApp client stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) #sending the connect signal stack.loop()
def main(messages): logging.debug("Starting up...") layers = ( (TextMessageSender, AckReceiver), (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer) ) + YOWSUP_CORE_LAYERS stack = YowStack(layers) stack.setProp(TextMessageSender.MESSAGES, messages) # setting credentials stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, CREDENTIALS) # whatsapp server address stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) # info about us as WhatsApp client stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) # sending the connect signal stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) stack.loop() # this is the program mainloop
def run(self): mess = None to = None phone = None passw = None with open(appPath + "/config.cfg") as f: for line in f: if line.find("phone number:", 0) == 0: phone = line[13:] phone = phone.strip() elif line.find("password:"******"send_message_startup:", 0) == 0: mess = line[21:-1] mess = "'" + mess + "'" elif line.find("allowed1:", 0) == 0: to = line[9:-1] CREDENTIALS = (phone, passw) print("/send.py " + to + " " + mess) p = subprocess.Popen(appPath + "/send.py " + to + " " + mess, stdout=subprocess.PIPE, shell=True) (output, err) = p.communicate() while True: layers = ( DaemonLayer, ( YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, ), ) + YOWSUP_CORE_LAYERS stack = YowStack(layers) stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, CREDENTIALS) stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) interface = stack.getLayer(6) interface.initialize(appPath) try: stack.loop() except: pass
class YowsupEchoStack(object): def __init__(self, credentials, encryptionEnabled = False): if encryptionEnabled: layers = ( EchoLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer, YowIqProtocolLayer, YowCallsProtocolLayer), YowAxolotlLayer, YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) else: env.CURRENT_ENV = env.S40YowsupEnv() layers = ( EchoLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer, YowIqProtocolLayer, YowCallsProtocolLayer), YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) self.stack = YowStack(layers) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, credentials) self.stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) self.stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) self.stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) def start(self): while True: try: self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) self.stack.loop() except AuthError as e: break except Exception as e: print(e) time.sleep(20)
def create_stack(self): CREDENTIALS = (PHONE, "F5vqaa/9jCXDDMoTSWysyhOI0BU=") # replace with your phone and password layers = ( EchoLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer), YowAxolotlLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) stack = YowStack(layers) stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, CREDENTIALS) # setting credentials stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) # whatsapp server address stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) # info about us as WhatsApp client stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) # sending the connect signal return stack
class YowsupCliStack(object): def __init__(self, credentials): self.stack = YowStack( (YowsupCliLayer,) + YOWSUP_FULL_STACK ) self.stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) self.stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) self.stack.setProp(YowCoderLayer.PROP_RESOURCE, YowConstants.RESOURCE) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, credentials) def start(self): print("Yowsup Cli client\n==================\nType /help for available commands\n") self.stack.broadcastEvent(YowLayerEvent(YowsupCliLayer.EVENT_START)) try: self.stack.loop(timeout = 0.5, discrete = 0.5) except AuthError as e: print("Auth Error, reason %s" % e) except KeyboardInterrupt: print("\nYowsdown") sys.exit(0)
class YowsupListenStack(object): def __init__(self, credentials, queue, encryptionEnabled = False): ListenLayer.setqueue(queue) if encryptionEnabled: layers = ( ListenLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer), YowAxolotlLayer, YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) else: env.CURRENT_ENV = env.S40YowsupEnv() layers = ( ListenLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer), YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) self.stack = YowStack(layers) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, credentials) self.stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) self.stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) self.stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) def start(self): self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) try: self.stack.loop() except AuthError as e: print("Authentication Error: %s" % e.message)
def main(): layers = ( EchoLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer), YowAxolotlLayer, YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) thread = MyThread(YowStack(layers)) try: print ("Starting "+sys.argv[1]) stack = YowStack(layers) stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, CREDENTIALS) #setting credentials stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) #whatsapp server address stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) #info about us as WhatsApp client stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) #sending the connect signal thread = MyThread(stack) thread.start() stack.loop(timeout = 0.5, discrete = 0.5) print("Exit "+sys.argv[1]+"!!") except Exception as e: print("Error in "+sys.argv[1]+", reason %s" % e) finally: thread.finish() time.sleep(5) print("Restart "+sys.argv[1]+"!!") #os.system("python run-Executi.py") sys.exit(0)
class YowsupEchoStack(object): def __init__(self, credentials): layers = ( EchoLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer), YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) self.stack = YowStack(layers) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, credentials) self.stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) self.stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) self.stack.setProp(YowCoderLayer.PROP_RESOURCE, YowConstants.RESOURCE) def start(self): self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) try: self.stack.loop() except AuthError as e: print("Authentication Error: %s" % e.message)
def whatsAppClient(): global globWhatsAppContinue, globWhatsAppRestart global globWhatsAppMsgIn, globWhatsAppMsgInAvailable, globWhatsAppMsgInAvailableLock global globWhatsAppMsgOut, globWhatsAppMsgOutType, globIWhatsAppImgOut, globWhatsAppMsgOutAvailable, globWhatsAppMsgOutAvailableLock global globDoMotionDetection, globWhatsAppSendPicture # YowAxolotlLayer added to prevent 'Unimplemented notification type "encrypt"' crash. layers=(SendReceiveLayer,)+(YOWSUP_PROTOCOL_LAYERS_FULL,YowAxolotlLayer)+YOWSUP_CORE_LAYERS stack = YowStack(layers) stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, credentials()) #setting credentials stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) #whatsapp server address stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) #info about us as WhatsApp client stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) #sending the connect signal while globWhatsAppContinue: try: globWhatsAppMsgOutAvailableLock.acquire() if globWhatsAppMsgOutAvailable: # Copy to keep critical section as short as possible. msgOutType = globWhatsAppMsgOutType if msgOutType == 'Image': # Copy image to whatsapp_client_img.jpg so this image can be sent without another thread # overwriting the image. imgOut = '/home/pi/DFRobotUploads/whatsapp_client_img.jpg' shutil.copy(globIWhatsAppImgOut, imgOut) msgOut = globWhatsAppMsgOut globWhatsAppMsgOutAvailable = False globWhatsAppMsgOutAvailableLock.release() if msgOutType == 'Image': messages = [(["31613484264", imgOut, msgOut])] else: messages = [(["31613484264", '', msgOut])] stack.setProp(SendReceiveLayer.PROP_MESSAGES,messages) stack.broadcastEvent(YowLayerEvent(SendReceiveLayer.EVENT_SEND_MESSAGE)) else: globWhatsAppMsgOutAvailableLock.release() # stack.loop() will call asyncore.loop() for an asynchronous I/O (socket) polling loop, # to poll for WhatsApp I/O. WhatsApp uses non-SSL data over SSL port 443. # The count parameter indicates the number of passes of the polling loop for each I/O channel. # The timeout argument sets the timeout parameter for the appropriate select() or poll() call, # measured in seconds. It indicates the wait time (blocking) for an I/O channel (file descriptor) # to become ready. stack.loop(timeout = 1.0, count=1) # Sleep to save cpu. time.sleep(1.0) # Update charging status of batteries. own_util.checkCharging() # Handle messages. msg = receiveWhatsAppMsg() if re.search('.*motion.*on.*', msg, re.IGNORECASE): globDoMotionDetection = True sendWhatsAppMsg('motion detection is on') elif re.search('.*motion.*off.*', msg, re.IGNORECASE): globDoMotionDetection = False sendWhatsAppMsg('motion detection is off') elif re.search('picture', msg, re.IGNORECASE): globWhatsAppSendPicture = True; elif re.search('hi', msg, re.IGNORECASE): sendWhatsAppMsg('hi there!') elif re.search('.*feel.*', msg, re.IGNORECASE): level = own_util.getBatteryLevel() if level != 'unknown': if int(level) > 190: sendWhatsAppMsg('I feel great, my energy level is ' + level) elif int(level) > 170: sendWhatsAppMsg('I feel ok, my energy level is ' + level) else: sendWhatsAppMsg('I feel a bit weak, my energy level is ' + level) else: sendWhatsAppMsg('I am not sure, my energy level is unknown') elif re.search('how are you', msg, re.IGNORECASE): sendWhatsAppMsg('I am fine, thx for asking!') elif re.search('battery', msg, re.IGNORECASE): charging = own_util.checkCharging() if charging == True: sendWhatsAppMsg('I am charging, my battery level is ' + own_util.getBatteryLevel()) else: sendWhatsAppMsg('I am not charging, my battery level is ' + own_util.getBatteryLevel()) elif re.search('awake', msg, re.IGNORECASE): sendWhatsAppMsg('I am awake for ' + own_util.getUptime()) elif re.search('joke', msg, re.IGNORECASE): sendWhatsAppMsg('\'What does your robot do, Sam?\' .......... \'It collects data about the surrounding environment, then discards it and drives into walls\'') elif msg != '': sendWhatsAppMsg('no comprendo: ' + msg) except Exception,e: logging.getLogger("MyLog").info('whatsAppClient exception: ' + str(e)) # Signal to restart this whatsAppClient thread. globWhatsAppRestart = True
class YowsupSendStack(object): def __init__(self, credentials, messages, encryptionEnabled=False): """ :param credentials: :param messages: list of (jid, message) tuples :param encryptionEnabled: :return: """ if encryptionEnabled: from yowsup.layers.axolotl import YowAxolotlLayer layers = (SendLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer), YowAxolotlLayer, YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer) else: layers = (SendLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer), YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer) self.stack = YowStack(layers) self.stack.setProp(SendLayer.PROP_MESSAGES, messages) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_PASSIVE, True) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, credentials) self.stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) self.stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) self.stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) def start(self): self.stack.broadcastEvent( YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) try: self.stack.loop() except AuthError as e: print("Authentication Error: %s" % e.message)
class SendMediaStack(object): def __init__(self, credentials, messages): layers=(SendMediaLayer,)+(YOWSUP_PROTOCOL_LAYERS_FULL,)+YOWSUP_CORE_LAYERS self.stack=YowStack(layers) self.stack.setProp(SendMediaLayer.PROP_MESSAGES,messages) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_PASSIVE,True) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS,credentials) self.stack.setProp(YowNetworkLayer.PROP_ENDPOINT,YowConstants.ENDPOINTS[0]) self.stack.setProp(YowCoderLayer.PROP_DOMAIN,YowConstants.DOMAIN) self.stack.setProp(YowCoderLayer.PROP_RESOURCE,env.CURRENT_ENV.getResource()) def start(self): self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) try: self.stack.loop() except AuthError as e: print("Authentication Error: %s" % e.message)
from yowsup.layers.network import YowNetworkLayer from yowsup.layers.protocol_messages import YowMessagesProtocolLayer from yowsup.layers.protocol_media import YowMediaProtocolLayer from yowsup.layers.stanzaregulator import YowStanzaRegulator from yowsup.layers.protocol_receipts import YowReceiptProtocolLayer from yowsup.layers.protocol_acks import YowAckProtocolLayer from yowsup.layers.logger import YowLoggerLayer from yowsup.layers.axolotl import YowAxolotlLayer from yowsup.common import YowConstants from yowsup import env credentials = ('972532351883', 'CXq89zg1QB6UYHRQ2BG0zPbcxlQ=') if __name__ == "__main__": env.CURRENT_ENV = env.S40YowsupEnv() layers = (EchoLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer),YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer) stack = YowStack(layers) stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, credentials) stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) stack.loop()
class Client(object): def __init__(self, login, password): self.login = login self.password = password def _send_message(self, to, message, is_media=False): layers = (SendLayer,) + (YOWSUP_PROTOCOL_LAYERS_FULL,) + YOWSUP_CORE_LAYERS self.stack = YowStack(layers) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_PASSIVE, True) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, (self.login, self.password)) self.stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) self.stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) self.stack.setProp(YowCoderLayer.PROP_RESOURCE, env.YowsupEnv.getCurrent().getResource()) self.stack.setProp(SendLayer.PROP_MESSAGES, [([to, message, is_media])]) self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) try: self.stack.loop() except KeyboardInterrupt: pass def send_message(self, to, message): self._send_message(to, message) def send_media(self, to, path): self._send_message(to, path, is_media=True)
def main(): ''' Console script to start the bot or output default config file. ''' # Arguments parser = argparse.ArgumentParser( description=_('Start Tombot, a chatbot for Whatsapp') ) parser.add_argument( '-v', '--verbose', help=_("enable debug logging"), action='store_true') parser.add_argument( '-d', '--dry-run', help=_("don't actually start bot, but print config"), action='store_true') parser.add_argument( '-p', '--poke', action='store_true', help=_("do everything except connecting (for testing)")) parser.add_argument('configfile', help=_("config file location"), nargs='?') args = parser.parse_args() # Set up logging if args.verbose: loglevel = logging.DEBUG else: loglevel = logging.INFO logging.basicConfig( level=loglevel, format='%(levelname)s - %(name)s - %(message)s') # Read configuration specpath = os.path.join(os.path.dirname(__file__), 'configspec.ini') config = ConfigObj(args.configfile, configspec=specpath) if not args.dry_run: config.validate(Validator()) if not args.configfile: error = _("You must specify a config file!") generate_hint = _("Generate one using 'tombot -d > file.ini'.") logging.critical(error) logging.critical(generate_hint) sys.exit(1) # Build scheduler, will be started in the bot's layer once connected jobstores = { 'default' : SQLAlchemyJobStore(url='sqlite:///jobs.sqlite'), } scheduler = BackgroundScheduler(jobstores=jobstores) # Build Yowsup stack credentials = (config['Yowsup']['username'], config['Yowsup']['password']) bot = TomBotLayer(config, scheduler) layers = ( bot, YowParallelLayer([ YowPresenceProtocolLayer, YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowIqProtocolLayer, YowReceiptProtocolLayer, YowChatstateProtocolLayer, YowAckProtocolLayer, YowGroupsProtocolLayer ]), YowAxolotlLayer, ) + YOWSUP_CORE_LAYERS stack = YowStack(layers) # Set credentials, server address, info about us as WhatsApp client stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, credentials) stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) # Send connect signal if we aren't poking at the bot if not args.poke: stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) stack.loop(timeout=0.5, discrete=0.5) #this is the program mainloop else: code.interact(local=locals()) else: # Output config file to stdout config.filename = None config.validate(Validator(), copy=True) output = config.write() for line in output: print(line)
def send_message(destination, message): ''' destination is <phone number> without '+' and with country code of type string, message is string e.g send_message('11133434343','hello') ''' messages = [(destination, message)] layers = (EchoLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowPresenceProtocolLayer) ) + YOWSUP_CORE_LAYERS stack = YowStack(layers) stack.setProp(EchoLayer.PROP_MESSAGES, messages) stack.setProp(YowAuthenticationProtocolLayer.PROP_PASSIVE, True) # Setting credentials stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, CREDENTIALS) # WhatsApp server address stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) # Sending connecting signal stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) try: # Program main loop stack.loop() except AuthError as e: print('Authentication error %s' % e.message) sys.exit(1)
class YowsupSyncStack(object): def __init__(self, credentials, contacts, encryptionEnabled = False): """ :param credentials: :param contacts: list of [jid ] :param encryptionEnabled: :return: """ if encryptionEnabled: layers = ( SyncLayer, (YowAuthenticationProtocolLayer, YowContactsIqProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer), YowAxolotlLayer, YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) else: env.CURRENT_ENV = env.S40YowsupEnv() layers = ( SyncLayer, (YowAuthenticationProtocolLayer, YowContactsIqProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer), YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) self.stack = YowStack(layers) self.stack.setProp(SyncLayer.PROP_CONTACTS, contacts) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_PASSIVE, True) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, credentials) self.stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) self.stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) self.stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) def start(self): self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) try: self.stack.loop() except AuthError as e: print("Authentication Error: %s" % e.message)
class Whatsapp(): def __init__(self, smarthome, account, password, trusted=None, logic='Whatsapp', cli_mode='False'): # Set Plugin Attributes self._sh = smarthome self._credentials = (account, password.encode('utf-8')) self._trusted = trusted self._logic = logic self._run = False if cli_mode == 'True': self._cli_mode = True return else: self._cli_mode = False # Yowsup Layers self._layers = ( SmarthomeLayer, YowParallelLayer((YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowIbProtocolLayer, YowIqProtocolLayer, YowNotificationsProtocolLayer, YowPresenceProtocolLayer)), YowCoderLayer, # YowLoggerLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer) # Yowsup Properties self._stack = YowStack(self._layers) self._stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, self._credentials) self._stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) self._stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) self._stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) # Connect Yowsup self._stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) # Get SmarthomeLayer and set this Plugin self._SmarthomeLayer = self._stack.getLayer(len(self._stack._YowStack__stack) - 1) self._SmarthomeLayer.setPlugin(self) def run(self): self._run = True if self._cli_mode == True: return try: self._stack.loop(timeout = 0.5, discrete = 0.5) except AuthError as e: logger.info("Authentication Error!") def stop(self): self._run = False if self._cli_mode == True: return logger.info("Shutting Down WhatsApp Client") disconnectEvent = YowLayerEvent(YowNetworkLayer.EVENT_STATE_DISCONNECT) self._stack.broadcastEvent(disconnectEvent) def parse_item(self, item): return None def parse_logic(self, logic): pass def __call__(self, message, phoneNumber=None): if phoneNumber == None: try: phoneNumber = self._trusted.split(' ')[0] except AttributeError: logger.error("Error sending Whatsapp Message") except IndentationError: logger.error("Error sending Whatsapp Message") if self._cli_mode == True: subprocess.call(['/usr/local/bin/yowsup-cli', 'demos', '-l', self._credentials[0] + ':' + self._credentials[1].decode(), '-s', phoneNumber, message]) return self._SmarthomeLayer.sendMsg(message, phoneNumber) def message_received(self, message, sender): try: self._sh.trigger(name=self._logic, value=message, source=sender.split('@')[0]) except AttributeError: logger.error("Error receiving Whatsapp Message") except IndentationError: logger.error("Error receiving Whatsapp Message")
class YowsupSendVCardStack(object): def __init__(self, credentials, to): """ :param credentials: :param to: :param encryptionEnabled: :return: """ layers = ( SendVCardLayer, (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer), YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) self.stack = YowStack(layers) self.stack.setProp(SendVCardLayer.PROP_TO, to) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_PASSIVE, True) self.stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, credentials) self.stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) self.stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) self.stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) def start(self): self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) try: self.stack.loop() except AuthError as e: print("Authentication Error: %s" % e.message)
from yowsup.layers.protocol_receipts import YowReceiptProtocolLayer from yowsup.layers.protocol_acks import YowAckProtocolLayer from yowsup.layers.network import YowNetworkLayer from yowsup.layers.coder import YowCoderLayer from yowsup.layers.protocol_iq import YowIqProtocolLayer from yowsup.stacks import YowStack from yowsup.common import YowConstants from yowsup.layers import YowLayerEvent from yowsup.stacks import YowStack, YOWSUP_CORE_LAYERS from yowsup.layers.axolotl import YowAxolotlLayer from yowsup.env import YowsupEnv CREDENTIALS = ("CC9999999999", "password-here") # replace with your phone and password if __name__== "__main__": layers = ( EchoLayer, YowParallelLayer([YowAuthenticationProtocolLayer, YowIqProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer]),YowAxolotlLayer ) + YOWSUP_CORE_LAYERS stack = YowStack(layers) stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, CREDENTIALS) #setting credentials stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) #whatsapp server address stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) stack.setProp(YowCoderLayer.PROP_RESOURCE, YowsupEnv.getCurrent().getResource()) #info about us as WhatsApp client stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) #sending the connect signal stack.loop() #this is the program mainloop
YowParallelLayer( [YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowMediaProtocolLayer, YowPresenceProtocolLayer, YowIqProtocolLayer, YowCallsProtocolLayer]), AxolotlReceivelayer, AxolotlControlLayer, AxolotlSendLayer, YowLoggerLayer, YowCoderLayer, YowCryptLayer, YowStanzaRegulator, YowNetworkLayer ) + YOWSUP_CORE_LAYERS stack = YowStack(layers) # Setting credentials stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, CREDENTIALS) # WhatsApp server address stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) stack.setProp(YowCoderLayer.PROP_RESOURCE, YowsupEnv.getCurrent().getResource()) # Sending connecting signal stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) print('Iniciado.') try: # Program main loop stack.loop() except KeyboardInterrupt as e: print('Saindo...') else: