Ejemplo n.º 1
0
 def onSuccess(self, successProtocolEntity):
     self.lock.acquire()
     
     for target in self.getProp(self.__class__.PROP_MESSAGES, []):
         #getProp() is trying to retreive the list of (jid, message) tuples, if none exist, use the default []
         print "@@@@@@", target
         print "mmmmm", "Spiniiiiiii"
         phone, message = target
         #phone = target
         #message = "Spiniiiii"
         #receipt = OutgoingReceiptProtocolEntity()
         if '@' in phone:
             messageEntity = TextMessageProtocolEntity(message, to = phone)
         elif '-' in phone:
             messageEntity = TextMessageProtocolEntity(message, to = "*****@*****.**" % phone)
         else:
             print "qqqqqq"
             messageEntity = TextMessageProtocolEntity(message, to = "*****@*****.**" % phone)
         receipt = IncomingReceiptProtocolEntity(messageEntity.getId(), phone, 'read', messageEntity.getParticipant())
         entity = IncomingAckProtocolEntity(receipt.getId(), 'receipt', phone, int(time.time()))
         print entity.getId()
         print "-->>", messageEntity.getId(), messageEntity.getFrom(), messageEntity.getParticipant()
         print "###", receipt.getId(), "type", receipt.getType()
         #append the id of message to ackQueue list
         #which the id of message will be deleted when ack is received.
         self.ackQueue.append(messageEntity.getId())
         self.toLower(messageEntity)
        # self.toLower(receipt)
     self.lock.release()
     print "lock released"
Ejemplo n.º 2
0
    def send(self, phone, message):
        self.lock.acquire()
        messageEntity = TextMessageProtocolEntity(message.encode('utf8'), to = self.normalizeJid(phone))

        self.ackQueue.append(messageEntity.getId())
        self.toLower(messageEntity)
        self.lock.release()
Ejemplo n.º 3
0
 def send(self, job, session):
     messageEntity = TextMessageProtocolEntity(job.args.encode('utf8'), to="*****@*****.**" % job.targets)
     job.whatsapp_message_id = messageEntity.getId()
     job.runs += 1
     job.sent = True
     session.commit()
     self.toLower(messageEntity)
Ejemplo n.º 4
0
 def onSuccess(self, successProtocolEntity):
     for target in self.getProp(self.__class__.PROP_MESSAGES, []):
         phone, message, message_type = target
         if int(message_type) == config.MESSAGE:
             print "Sending message"
             if '@' in phone:
                 messageEntity = TextMessageProtocolEntity(message, to = phone)
             elif '-' in phone:
                 messageEntity = TextMessageProtocolEntity(message, to = "*****@*****.**" % phone)
             else:
                 messageEntity = TextMessageProtocolEntity(message, to = "*****@*****.**" % phone)
             self.ackQueue.append(messageEntity.getId())
             self.toLower(messageEntity)
         elif int(message_type) == config.IMAGE:
             print "Sending Image"
             if '@' in phone:
                 jid = phone
             elif '-' in phone:
                 jid = "*****@*****.**" % phone
             else:
                 jid = "*****@*****.**" % phone
             path = message
             print "Sending image to : " + jid + " with : " + path
             entity = RequestUploadIqProtocolEntity(RequestUploadIqProtocolEntity.MEDIA_TYPE_IMAGE, filePath=path)
             successFn = lambda successEntity, originalEntity: self.onRequestUploadResult(jid, path, successEntity, originalEntity)
             errorFn = lambda errorEntity, originalEntity: self.onRequestUploadError(jid, path, errorEntity, originalEntity)
             self._sendIq(entity, successFn, errorFn)
Ejemplo n.º 5
0
    def onMessage(self, messageProtocolEntity):
        text = ''
        if messageProtocolEntity.getType() == 'text':
            self.onTextMessage(messageProtocolEntity)
            text = messageProtocolEntity.getBody()
        elif messageProtocolEntity.getType() == 'media':
            self.onMediaMessage(messageProtocolEntity)
            text = 'Rich media'
        else:
            text = messageProtocolEntity.getType()

        print("Our url is %s" % self.url)

        self.lock.acquire()

        fullUrl = self.url

        params = urllib.urlencode({'to': self.our_number, 'from': messageProtocolEntity.getFrom(), 'message': text, 'mcc': '', 'mnc': ''})

        response = urllib2.urlopen(fullUrl + "&%s" % params)
        html = response.read()
        messageEntity = TextMessageProtocolEntity(html, to=messageProtocolEntity.getFrom())


        self.ackQueue.append(messageEntity.getId())
        self.toLower(messageEntity)

        # self.toLower(messageProtocolEntity.forward(messageProtocolEntity.getFrom()))
        self.toLower(messageProtocolEntity.ack())
        self.toLower(messageProtocolEntity.ack(True))

        self.lock.release()
Ejemplo n.º 6
0
	def onSuccess(self, successProtocolEntity):
		self.lock.acquire()
		self.phone, self.message= self.getProp(self.__class__.PROP_CONTENT)
		messageEntity = TextMessageProtocolEntity(self.message.encode('utf-8'), to = "*****@*****.**" % self.phone)
		self.ackQueue.append(messageEntity.getId())
		self.toLower(messageEntity)
		self.lock.release()
Ejemplo n.º 7
0
    def onMedia(self, messageProtocolEntity):
        print 'received media:', messageProtocolEntity

        f = messageProtocolEntity.getFrom()
        print  '-----', f
        if messageProtocolEntity.getMediaType() == "image":
            receipt = OutgoingReceiptProtocolEntity(messageProtocolEntity.getId(), messageProtocolEntity.getFrom(), "read")

            recognized_categories = ir.recognizeImage(messageProtocolEntity.getMediaUrl(),3)

            outgoingMessageProtocolEntity =  TextMessageProtocolEntity(
                'Sua mensagem foi recebida como: %s' % catConvert[recognized_categories[0]], to = messageProtocolEntity.getFrom())

            receipt_dic[outgoingMessageProtocolEntity.getId()] = receipt

            self.toLower(outgoingMessageProtocolEntity)

            tags = ir.tagsForImage(messageProtocolEntity.getMediaUrl(),20)

            saveNewImage(url=messageProtocolEntity.url, jid=messageProtocolEntity.getFrom(),
                categories=recognized_categories, caption=messageProtocolEntity.getCaption(), tags=tags)

            #image = Image(url=messageProtocolEntity.url, jid=messageProtocolEntity.getFrom(), categories=categories,
            #                caption=messageProtocolEntity.getCaption(), tags=tags)
            #image.save()

            self.sendImageToSubscribers(messageProtocolEntity, catConvert[recognized_categories[0]])
Ejemplo n.º 8
0
    def send(self, phone, message):
        self.lock.acquire()
        messageEntity = TextMessageProtocolEntity(message.encode('utf8'),
                                                  to=self.normalizeJid(phone))

        self.ackQueue.append(messageEntity.getId())
        self.toLower(messageEntity)
        self.lock.release()
Ejemplo n.º 9
0
 def onSuccess(self, successProtocolEntity):
     for target in self.getProp(self.__class__.PROP_MESSAGES, []):
         phone, message = target
         messageEntity = TextMessageProtocolEntity(message, to=phone)
         receipt = IncomingReceiptProtocolEntity(messageEntity.getId(), phone, int(time.time()))
         self.toLower(messageEntity)
         print "Testing"
         self.toLower(receipt)
Ejemplo n.º 10
0
 def onSuccess(self, successProtocolEntity):
     self.lock.acquire()
     for target in self.getProp(self.__class__.PROP_MESSAGES, []):
         phone, message = target
         messageEntity = TextMessageProtocolEntity(message, to = "*****@*****.**" % phone)
         self.ackQueue.append(messageEntity.getId())
         self.toLower(messageEntity)
     self.lock.release()
Ejemplo n.º 11
0
 def SendTextMessage(self, gid, name, message):
     self.markOnline()
     self.waitHumanRandom()
     self.makeTyping(gid)
     self.waitHumanRandom()
     protocolEntity = TextMessageProtocolEntity(message, to = gid, notify = name)
     self.toLower(protocolEntity)
     self.makeStopTyping(gid)
     self.pending_sends[protocolEntity.getId()] = protocolEntity
Ejemplo n.º 12
0
 def onSuccess(self, successProtocolEntity):
     for target in self.getProp(self.__class__.PROP_MESSAGES, []):
         phone, message = target
         messageEntity = TextMessageProtocolEntity(message, to=phone)
         receipt = IncomingReceiptProtocolEntity(messageEntity.getId(),
                                                 phone, int(time.time()))
         self.toLower(messageEntity)
         print "Testing"
         self.toLower(receipt)
Ejemplo n.º 13
0
 def send(self, job, session):
     messageEntity = TextMessageProtocolEntity(job.args.encode('utf8'),
                                               to="*****@*****.**" %
                                               job.targets)
     job.whatsapp_message_id = messageEntity.getId()
     job.runs += 1
     job.sent = True
     session.commit()
     self.toLower(messageEntity)
Ejemplo n.º 14
0
 def handle_outbound_message(self, message):
     # message is a vumi.message.TransportUserMessage
     self.log.info('Sending message: %s' % (message.to_json(),))
     msg = TextMessageProtocolEntity(
         message['content'].encode("UTF-8"),
         to=msisdn_to_whatsapp(message['to_addr']).encode("UTF-8"))
     self.redis.setex(
         msg.getId(), self.config.ack_timeout, message['message_id'])
     self.stack_client.send_to_stack(msg)
Ejemplo n.º 15
0
 def onIq(self, entity):
     self.lock.acquire()
     for alert in self.alertQueue:
         message = alert['alert_text']
         messageEntity = TextMessageProtocolEntity(message, to = Jid.normalize(self.admin_phone))
         ack_id = messageEntity.getId()
         self.ackQueue.append(ack_id)
         self.sentQueue[ack_id] = alert;
         print(alert)
     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 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()
Ejemplo n.º 18
0
 def onSuccess(self, successProtocolEntity):
     self.lock.acquire()
     for target in self.getProp(self.__class__.PROP_MESSAGES, []):
         phone, message = target
         messageEntity = TextMessageProtocolEntity(message,
                                                   to="*****@*****.**" %
                                                   phone)
         self.ackQueue.append(messageEntity.getId())
         self.toLower(messageEntity)
     self.lock.release()
Ejemplo n.º 19
0
 def onSuccess(self, successProtocolEntity):
     self.lock.acquire()
     for target in self.getProp(self.__class__.PROP_MESSAGES, []):
         #getProp() is trying to retreive the list of (jid, message) tuples, if none exist, use the default []
         phone, message = target
         messageEntity = TextMessageProtocolEntity(message, to = Jid.normalize(phone))
         #append the id of message to ackQueue list
         #which the id of message will be deleted when ack is received.
         self.ackQueue.append(messageEntity.getId())
         self.toLower(messageEntity)
     self.lock.release()
Ejemplo n.º 20
0
 def onSuccess(self, entity):
     logging.info("Successfully connected. Sending messages")
     for message in self.getProp(self.__class__.MESSAGES, []):
         logging.info(message)
         outgoingMessage = TextMessageProtocolEntity(
             message.text,
             to=message.to
         )
         logging.info("Sending Message ID: {0}".format(outgoingMessage.getId()))
         self.toLower(outgoingMessage)
         logging.info("Sent!")
Ejemplo n.º 21
0
 def onSuccess(self, successProtocolEntity):
     self.lock.acquire()
     for target in self.getProp(self.__class__.PROP_MESSAGES, []):
         #getProp() is trying to retreive the list of (jid, message) tuples, if none exist, use the default []
         phone, message = target
         messageEntity = TextMessageProtocolEntity(message, to = Jid.normalize(phone))
         #append the id of message to ackQueue list
         #which the id of message will be deleted when ack is received.
         self.ackQueue.append(messageEntity.getId())
         self.toLower(messageEntity)
     self.lock.release()
Ejemplo n.º 22
0
    def onSuccess(self, successProtocolEntity):
        self.lock.acquire()

        print("Succesfully connected to the WhatsApp service.")

        for recipient in settings.DEFAULT_RECIPIENTS:
            print("Sending to " + recipient)
            messageEntity = TextMessageProtocolEntity(self.generateNotificationText(), to=recipient)
            self.queue.append(messageEntity.getId())
            self.toLower(messageEntity)

        self.lock.release()
Ejemplo n.º 23
0
 def onIq(self, entity):
     self.lock.acquire()
     for alert in self.alertQueue:
         message = alert['alert_text']
         messageEntity = TextMessageProtocolEntity(message,
                                                   to=Jid.normalize(
                                                       self.admin_phone))
         ack_id = messageEntity.getId()
         self.ackQueue.append(ack_id)
         self.sentQueue[ack_id] = alert
         print(alert)
     self.lock.release()
Ejemplo n.º 24
0
    def onSuccess(self, successProtocolEntity):
        self.lock.acquire()

        print("Succesfully connected to the WhatsApp service.")

        for recipient in settings.DEFAULT_RECIPIENTS:
            print("Sending to " + recipient)
            messageEntity = TextMessageProtocolEntity(
                self.generateNotificationText(), to=recipient)
            self.queue.append(messageEntity.getId())
            self.toLower(messageEntity)

        self.lock.release()
Ejemplo n.º 25
0
 def test_on_message(self):
     content = "Received message"
     jid = "*****@*****.**"
     with mock.patch("yowsup.layers.YowLayer.getStack", callable=mock.MagicMock()) as mock_stack:
         mock_stack.return_value.caller.on_message = mock.MagicMock()
         msg = TextMessageProtocolEntity(content, _from=jid)
         self.receive(msg)      
         msg_to_upper = self.upperSink.pop()  
         self.assertEqual(msg_to_upper.getFrom(), jid)
         self.assertEqual(msg_to_upper.getBody(), content)
         self.assertEqual(mock_stack.return_value.caller.on_message.call_count, 1)
         args, kwargs = mock_stack.return_value.caller.on_message.call_args
         self.assertEqual(args[0], msg.getId())
         self.assertEqual(args[1], jid)
         self.assertEqual(args[2], content)
Ejemplo n.º 26
0
    def onSuccess(self, successProtocolEntity):
        self.lock.acquire()

        for target in self.getProp(self.__class__.PROP_MESSAGES, []):
            #getProp() is trying to retreive the list of (jid, message) tuples, if none exist, use the default []
            print "@@@@@@", target
            print "mmmmm", "Spiniiiiiii"
            phone, message = target
            #phone = target
            #message = "Spiniiiii"
            #receipt = OutgoingReceiptProtocolEntity()
            if '@' in phone:
                messageEntity = TextMessageProtocolEntity(message, to=phone)
            elif '-' in phone:
                messageEntity = TextMessageProtocolEntity(message,
                                                          to="*****@*****.**" % phone)
            else:
                print "qqqqqq"
                messageEntity = TextMessageProtocolEntity(
                    message, to="*****@*****.**" % phone)
            receipt = IncomingReceiptProtocolEntity(
                messageEntity.getId(), phone, 'read',
                messageEntity.getParticipant())
            entity = IncomingAckProtocolEntity(receipt.getId(), 'receipt',
                                               phone, int(time.time()))
            print entity.getId()
            print "-->>", messageEntity.getId(), messageEntity.getFrom(
            ), messageEntity.getParticipant()
            print "###", receipt.getId(), "type", receipt.getType()
            #append the id of message to ackQueue list
            #which the id of message will be deleted when ack is received.
            self.ackQueue.append(messageEntity.getId())
            self.toLower(messageEntity)
        # self.toLower(receipt)
        self.lock.release()
        print "lock released"
Ejemplo n.º 27
0
 def on_send_messages(self, yowLayerEvent):
     """
     Callback function when receiving event to send messages
     """
     for message in yowLayerEvent.getArg("messages"):
         number, content = message
         if "@" in number:
             message_protocol_entity = TextMessageProtocolEntity(content, to=number)
         elif "-" in number:
             message_protocol_entity = TextMessageProtocolEntity(content, to="*****@*****.**" % number)
         else:
             message_protocol_entity = TextMessageProtocolEntity(content, to="*****@*****.**" % number)
         # append the id of message to ack_pending list
         # which the id of message will be deleted when ack is received.
         self.ack_pending.append(message_protocol_entity.getId())
         self._send_protocol_entity(message_protocol_entity)
Ejemplo n.º 28
0
 def on_success(self, success_protocol_entity):
     self.lock.acquire()
     for target in self.getProp(self.__class__.PROP_MESSAGES, []):
         phone, message, is_media = target
         jid = "*****@*****.**" % phone
         if is_media:
             path = message
             entity = self.get_upload_entity(path)
             success_fn = lambda success, original: self.on_request_upload_result(jid, path, success, original)
             error_fn = lambda error, original: self.on_request_upload_error(jid, path, error, original)
             self._sendIq(entity, success_fn, error_fn)
         else:
             message_entity = TextMessageProtocolEntity(message, to=jid)
             self.ackQueue.append(message_entity.getId())
             self.toLower(message_entity)
     self.lock.release()
Ejemplo n.º 29
0
 def on_success(self, success_protocol_entity):
     self.lock.acquire()
     for target in self.getProp(self.__class__.PROP_MESSAGES, []):
         phone, message, is_media = target
         jid = "*****@*****.**" % phone
         if is_media:
             path = message
             entity = self.get_upload_entity(path)
             success_fn = lambda success, original: self.on_request_upload_result(jid, path, success, original)
             error_fn = lambda error, original: self.on_request_upload_error(jid, path, error, original)
             self._sendIq(entity, success_fn, error_fn)
         else:
             message_entity = TextMessageProtocolEntity(message, to=jid)
             self.ackQueue.append(message_entity.getId())
             self.toLower(message_entity)
     self.lock.release()
Ejemplo n.º 30
0
 def sendMessage(self, num, message):
     num = self.normalise(num)
     self.start_typing(num)
     time.sleep(random.uniform(0.1, 0.3))
     print('To :', num)
     messageEntity = TextMessageProtocolEntity(message, to=num)
     self.ackQueue.append(messageEntity.getId())
     try:
         self.toLower(messageEntity)
     except AttributeError:
         print('Ohi Errro')
     # print('Message: ',message,' To: ',num)
     self.stop_typing(num)
     time.sleep(random.uniform(0.1, 0.3))
     self.start_typing(num)
     time.sleep(0.1)
     self.stop_typing(num)
Ejemplo n.º 31
0
 def test_on_message(self):
     content = "Received message"
     jid = "*****@*****.**"
     with mock.patch("yowsup.layers.YowLayer.getStack",
                     callable=mock.MagicMock()) as mock_stack:
         mock_stack.return_value.caller.on_message = mock.MagicMock()
         msg = TextMessageProtocolEntity(content, _from=jid)
         self.receive(msg)
         msg_to_upper = self.upperSink.pop()
         self.assertEqual(msg_to_upper.getFrom(), jid)
         self.assertEqual(msg_to_upper.getBody(), content)
         self.assertEqual(
             mock_stack.return_value.caller.on_message.call_count, 1)
         args, kwargs = mock_stack.return_value.caller.on_message.call_args
         self.assertEqual(args[0], msg.getId())
         self.assertEqual(args[1], jid)
         self.assertEqual(args[2], content)
Ejemplo n.º 32
0
    def send_message(self, phone, message):
        """
        Sends message
        :param phone: Recipient of the message (phone or phone-group)
        :param message: Message to send
        :return: void
        """
        self.lock.acquire()
        if '@' in phone:  # full adress
            entity = TextMessageProtocolEntity(message, to=phone)
        elif '-' in phone:  # group
            entity = TextMessageProtocolEntity(message,
                                               to='{}@g.us'.format(phone))
        else:  # number only
            entity = TextMessageProtocolEntity(
                message, to='{}@s.whatsapp.net'.format(phone))

        self.ackQueue.append(entity.getId())
        self.toLower(entity)
        self.lock.release()