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"
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()
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)
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)
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()
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()
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]])
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()
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)
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()
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
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)
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()
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()
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()
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!")
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()
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 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()
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)
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"
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)
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()
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)
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)
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()