def onMediaMessage(self, messageProtocolEntity): if messageProtocolEntity.getMediaType() == "image": receipt = OutgoingReceiptProtocolEntity(messageProtocolEntity.getId(), messageProtocolEntity.getFrom()) outImage = ImageDownloadableMediaMessageProtocolEntity( messageProtocolEntity.getMimeType(), messageProtocolEntity.fileHash, messageProtocolEntity.url, messageProtocolEntity.ip, messageProtocolEntity.size, messageProtocolEntity.fileName, messageProtocolEntity.encoding, messageProtocolEntity.width, messageProtocolEntity.height, messageProtocolEntity.getCaption(), to = messageProtocolEntity.getFrom(), preview = messageProtocolEntity.getPreview()) print("Echoing image %s to %s" % (messageProtocolEntity.url, messageProtocolEntity.getFrom(False))) #send receipt otherwise we keep receiving the same message over and over self.toLower(receipt) self.toLower(outImage) elif messageProtocolEntity.getMediaType() == "location": receipt = OutgoingReceiptProtocolEntity(messageProtocolEntity.getId(), messageProtocolEntity.getFrom()) outLocation = LocationMediaMessageProtocolEntity(messageProtocolEntity.getLatitude(), messageProtocolEntity.getLongitude(), messageProtocolEntity.getLocationName(), messageProtocolEntity.getLocationURL(), messageProtocolEntity.encoding, to = messageProtocolEntity.getFrom(), preview=messageProtocolEntity.getPreview()) print("Echoing location (%s, %s) to %s" % (messageProtocolEntity.getLatitude(), messageProtocolEntity.getLongitude(), messageProtocolEntity.getFrom(False))) #send receipt otherwise we keep receiving the same message over and over self.toLower(outLocation) self.toLower(receipt) elif messageProtocolEntity.getMediaType() == "vcard": receipt = OutgoingReceiptProtocolEntity(messageProtocolEntity.getId(), messageProtocolEntity.getFrom()) outVcard = VCardMediaMessageProtocolEntity(messageProtocolEntity.getName(),messageProtocolEntity.getCardData(),to = messageProtocolEntity.getFrom()) print("Echoing vcard (%s, %s) to %s" % (messageProtocolEntity.getName(), messageProtocolEntity.getCardData(), messageProtocolEntity.getFrom(False))) #send receipt otherwise we keep receiving the same message over and over self.toLower(outVcard) self.toLower(receipt)
def location_send(self, number, lat, lon, name, url, encoding): outgoingMessage = LocationMediaMessageProtocolEntity( lat, lon, name, url, encoding, to=self.normalizeJid(number)) self.toLower(outgoingMessage)
def onMediaMessage(self, messageProtocolEntity): global lastSentId if messageProtocolEntity.getMediaType() == "image": receipt = OutgoingReceiptProtocolEntity( messageProtocolEntity.getId(), messageProtocolEntity.getFrom()) outImage = ImageDownloadableMediaMessageProtocolEntity( messageProtocolEntity.getMimeType(), messageProtocolEntity.fileHash, messageProtocolEntity.url, messageProtocolEntity.ip, messageProtocolEntity.size, messageProtocolEntity.fileName, messageProtocolEntity.encoding, messageProtocolEntity.width, messageProtocolEntity.height, messageProtocolEntity.getCaption(), to=messageProtocolEntity.getFrom(), preview=messageProtocolEntity.getPreview()) print("Echoing image %s to %s" % (messageProtocolEntity.url, messageProtocolEntity.getFrom(False))) #send receipt otherwise we keep receiving the same message over and over self.toLower(receipt) self.toLower(outImage) elif messageProtocolEntity.getMediaType() == "location": receipt = OutgoingReceiptProtocolEntity( messageProtocolEntity.getId(), messageProtocolEntity.getFrom()) outLocation = LocationMediaMessageProtocolEntity( messageProtocolEntity.getLatitude(), messageProtocolEntity.getLongitude(), messageProtocolEntity.getLocationName(), messageProtocolEntity.getLocationURL(), messageProtocolEntity.encoding, to=messageProtocolEntity.getFrom(), preview=messageProtocolEntity.getPreview()) print("Echoing location (%s, %s) to %s" % (messageProtocolEntity.getLatitude(), messageProtocolEntity.getLongitude(), messageProtocolEntity.getFrom(False))) #send receipt otherwise we keep receiving the same message over and over self.toLower(outLocation) self.toLower(receipt) elif messageProtocolEntity.getMediaType() == "vcard": receipt = OutgoingReceiptProtocolEntity( messageProtocolEntity.getId(), messageProtocolEntity.getFrom()) self.toLower(receipt) x = messageProtocolEntity.getCardData() received_vcard_number = x.split('TEL:')[1].split("\n")[0] #inserting in DB linkerObject = linker() whatsapp_id = messageProtocolEntity.getFrom(False) message_sender = linkerObject.returnEkoId(whatsapp_id, 0) message_body = received_vcard_number message_source = linkerObject.returnMessageSource() message_type = linkerObject.returnMessageType(2) message_status = linkerObject.returnMessageStatus() message_destination = message_source dbUpdateObject = dbUpdate() dbUpdateObject.insertReceiveMessage(message_sender, message_body, message_source, message_type, message_status) print("Receiving %s from %s" % (message_body, messageProtocolEntity.getFrom(False))) self.toLower(receipt) time.sleep(2) #to pick only unprocessed message replyToBeSent = dbUpdateObject.selectResponseMessage( message_destination, lastSentId) self.replyBack(messageProtocolEntity, replyToBeSent) elif messageProtocolEntity.getMediaType() == "audio": receipt = OutgoingReceiptProtocolEntity( messageProtocolEntity.getId(), messageProtocolEntity.getFrom()) self.toLower(receipt) outAudio = AudioDownloadableMediaMessageProtocolEntity( messageProtocolEntity.getMimeType(), messageProtocolEntity.fileHash, messageProtocolEntity.url, messageProtocolEntity.ip, messageProtocolEntity.size, messageProtocolEntity.fileName, messageProtocolEntity.abitrate, messageProtocolEntity.acodec, messageProtocolEntity.asampfreq, messageProtocolEntity.duration, messageProtocolEntity.encoding, messageProtocolEntity.origin, messageProtocolEntity.seconds, to=messageProtocolEntity.getFrom()) path = str(messageProtocolEntity.url) commands.getoutput('wget ' + path) fname = path.split('/')[-1] commands.getoutput('ffmpeg -i ' + fname + ' ' + fname.split('.')[0] + '.wav') r = sr.Recognizer() with sr.WavFile( fname.split('.')[0] + '.wav') as source: # use "test.wav" as the audio source audio = r.record(source) # extract audio data from the file try: print("Receiving sound. Transcription: " + r.recognize(audio) ) # recognize speech using Google Speech Recognition except LookupError: # speech is unintelligible print("Could not understand audio") return #inserting in DB whatsapp_id = messageProtocolEntity.getFrom(False) message_sender = linker.returnEkoId(whatsapp_id, 0) message_body = r.recognize(audio) message_source = linker.returnmessageSource() message_type = linker.returnmessageType(2) message_status = linker.returnmessagestatus() message_destination = message_source dbUpdateObject = dbUpdate() dbUpdateObject.insertReceiveMessage(message_sender, message_body, message_source, message_type, message_status) print( "Receiving %s from %s" % (r.recognize(audio), messageProtocolEntity.getFrom(False))) self.toLower(receipt) time.sleep(2) #to pick only unprocessed message replyToBeSent = dbUpdateObject.selectResponseMessage( message_destination, self.lastSentId) self.replyBack(messageProtocolEntity, replyToBeSent)
def onMediaMessage(self, messageProtocolEntity): if messageProtocolEntity.getMediaType() == "image": receipt = OutgoingReceiptProtocolEntity( messageProtocolEntity.getId(), messageProtocolEntity.getFrom()) outImage = ImageDownloadableMediaMessageProtocolEntity( messageProtocolEntity.getMimeType(), messageProtocolEntity.fileHash, messageProtocolEntity.url, messageProtocolEntity.ip, messageProtocolEntity.size, messageProtocolEntity.fileName, messageProtocolEntity.encoding, messageProtocolEntity.width, messageProtocolEntity.height, messageProtocolEntity.getCaption(), to=messageProtocolEntity.getFrom(), preview=messageProtocolEntity.getPreview()) print("image %s [%s]:%s" % (messageProtocolEntity.getFrom(False), datetime.datetime.fromtimestamp( messageProtocolEntity.getTimestamp()).strftime( '%d-%m-%Y %H:%M'), messageProtocolEntity.url)) elif messageProtocolEntity.getMediaType() == "location": receipt = OutgoingReceiptProtocolEntity( messageProtocolEntity.getId(), messageProtocolEntity.getFrom()) outLocation = LocationMediaMessageProtocolEntity( messageProtocolEntity.getLatitude(), messageProtocolEntity.getLongitude(), messageProtocolEntity.getLocationName(), messageProtocolEntity.getLocationURL(), messageProtocolEntity.encoding, to=messageProtocolEntity.getFrom(), preview=messageProtocolEntity.getPreview()) print("location %s [%s]:(%s, %s)" % (messageProtocolEntity.getFrom(False), datetime.datetime.fromtimestamp( messageProtocolEntity.getTimestamp()).strftime( '%d-%m-%Y %H:%M'), messageProtocolEntity.getLatitude(), messageProtocolEntity.getLongitude())) elif messageProtocolEntity.getMediaType() == "vcard": receipt = OutgoingReceiptProtocolEntity( messageProtocolEntity.getId(), messageProtocolEntity.getFrom()) outVcard = VCardMediaMessageProtocolEntity( messageProtocolEntity.getName(), messageProtocolEntity.getCardData(), to=messageProtocolEntity.getFrom()) print("vcard %s [%s]:(%s, %s)" % (messageProtocolEntity.getFrom(False), datetime.datetime.fromtimestamp( messageProtocolEntity.getTimestamp()).strftime( '%d-%m-%Y %H:%M'), messageProtocolEntity.getName(), messageProtocolEntity.getCardData())) outgoingMessageProtocolEntity = OutgoingReceiptProtocolEntity( messageProtocolEntity.getId(), messageProtocolEntity.getFrom()) self.toLower(receipt) print("Sent delivered receipt Message %s" % messageProtocolEntity.getId())