def onMessage(self, messageProtocolEntity): if messageProtocolEntity.getType() == 'text': time.sleep(0.5) elif messageProtocolEntity.getType() == 'media': time.sleep(0.5) time.sleep(0.5) self.toLower(messageProtocolEntity.ack()) #Set received (double v) time.sleep(0.5) self.toLower(PresenceProtocolEntity(name=name)) #Set name Presence time.sleep(0.5) self.toLower(AvailablePresenceProtocolEntity()) #Set online time.sleep(0.5) self.toLower( messageProtocolEntity.ack(True)) #Set read (double v blue) time.sleep(0.5) self.toLower( OutgoingChatstateProtocolEntity( OutgoingChatstateProtocolEntity.STATE_TYPING, Jid.normalize( messageProtocolEntity.getFrom(False)))) #Set is writing time.sleep(2) self.toLower( OutgoingChatstateProtocolEntity( OutgoingChatstateProtocolEntity.STATE_PAUSED, Jid.normalize( messageProtocolEntity.getFrom(False)))) #Set no is writing time.sleep(1) self.onTextMessage(messageProtocolEntity) #Send the answer time.sleep(3) self.toLower(UnavailablePresenceProtocolEntity()) #Set offline
def wolfram_cb(bot, message, *args, **kwargs): ''' (Attempt to) answer query using the WolframAlpha API. Results may not be interpreted as you'd expect, open link for explanation. ''' if not CLIENT: return _('Not connected to WolframAlpha!') query = extract_query(message) LOGGER.debug('Query to WolframAlpha: %s', query) entity = OutgoingChatstateProtocolEntity( ChatstateProtocolEntity.STATE_TYPING, message.getFrom()) bot.toLower(entity) result = CLIENT.query(query) entity = OutgoingChatstateProtocolEntity( ChatstateProtocolEntity.STATE_PAUSED, message.getFrom()) bot.toLower(entity) restext = _('Result from WolframAlpha:\n') results = [ p.text.encode('utf-8') for p in result.pods if p.title in ('Result', 'Value', 'Decimal approximation', 'Exact result') ] if not results: return _('No result.') restext += '\n'.join(results) + '\n' restext += 'Link: https://wolframalpha.com/input/?i={}'.format( urllib.quote(query).replace('%20', '+')) return restext
def onMessage(self, messageProtocolEntity): if messageProtocolEntity.getType() == 'text': time.sleep(random.randrange(Cfg.RAND_MIN,Cfg.RAND_MAX)) #Devine mensagem como enviada self.toLower(messageProtocolEntity.ack()) time.sleep(random.randrange(Cfg.RAND_MIN,Cfg.RAND_MAX)) self.toLower(AvailablePresenceProtocolEntity()) time.sleep(random.randrange(Cfg.RAND_MIN,Cfg.RAND_MAX)) self.toLower(PresenceProtocolEntity(name = name)) time.sleep(random.randrange(Cfg.RAND_MIN,Cfg.RAND_MAX)) #Devine mensagem como visualisada self.toLower(messageProtocolEntity.ack(True)) time.sleep(random.randrange(Cfg.RAND_MIN,Cfg.RAND_MAX)) self.toLower(OutgoingChatstateProtocolEntity(OutgoingChatstateProtocolEntity.STATE_TYPING, Jid.normalize(messageProtocolEntity.getFrom(False))))#Set esta escribiendo time.sleep(random.randrange(Cfg.RAND_MIN,Cfg.RAND_MAX)) #Envia a resposta self.onTextMessage(messageProtocolEntity) time.sleep(random.randrange(Cfg.RAND_MIN,Cfg.RAND_MAX)) self.toLower(OutgoingChatstateProtocolEntity(OutgoingChatstateProtocolEntity.STATE_PAUSED, Jid.normalize(messageProtocolEntity.getFrom(False))))#Set pausa time.sleep(random.randrange(Cfg.RAND_MIN,Cfg.RAND_MAX)) #Fica offline self.toLower(UnavailablePresenceProtocolEntity()) elif messageProtocolEntity.getType() == 'media': time.sleep(random.randrange(Cfg.RAND_MIN,Cfg.RAND_MAX)) self.toLower(AvailablePresenceProtocolEntity()) time.sleep(random.randrange(Cfg.RAND_MIN,Cfg.RAND_MAX)) self.toLower(PresenceProtocolEntity(name = name))#Set presencia self.onMediaMessage(messageProtocolEntity) self.toLower(messageProtocolEntity.ack()) self.toLower(messageProtocolEntity.ack(True)) time.sleep(random.randrange(Cfg.RAND_MIN,Cfg.RAND_MAX)) self.toLower(UnavailablePresenceProtocolEntity())#Set offline else: pass
def onMessage(self, messageProtocolEntity): is_admin = 0 global send_next send_next_ = send_next admin_number = '*****@*****.**' message = 'Whatsbot' Demo_Message = "*WhatsBot*: \nSend Whatsapp messages by chatting with our WhatsBot." receipt = OutgoingReceiptProtocolEntity( messageProtocolEntity.getId(), messageProtocolEntity.getFrom(), 'read', messageProtocolEntity.getParticipant()) time.sleep(0.5) self.toLower(receipt) print("Message from " + messageProtocolEntity.getFrom()) if (messageProtocolEntity.getFrom() == (admin_number)): print("IS admin number") if send_next_ == 1: list_cont = {'919417290392', '917508377911'} if messageProtocolEntity.getType() == 'text': try: if ((messageProtocolEntity.getBody() ).index('WhatsBot --end') == 0): send_next = 0 print("Sending Disabled") else: print("Sending Message") except: for contact_member in list_cont: entity = OutgoingChatstateProtocolEntity( ChatstateProtocolEntity.STATE_TYPING, contact_member + '@s.whatsapp.net') self.toLower(entity) self.toLower( messageProtocolEntity.forward( contact_member + '@s.whatsapp.net')) print("Sending to :", contact_member) else: print("Is media") #for contact_member in list_cont: #self.toLower(messageProtocolEntity.forward(contact_member+'@s.whatsapp.net')) #print ("Sending to :", contact_member) if messageProtocolEntity.getType() == 'text': print("Is Text") message = messageProtocolEntity.getBody() try: if ((messageProtocolEntity.getBody() ).index('WhatsBot --send') == 0): send_next = 1 print("Send Next ") except: print("Message Passed") else: outgoingMessageProtocolEntity = TextMessageProtocolEntity( Demo_Message, to=messageProtocolEntity.getFrom()) self.toLower(outgoingMessageProtocolEntity)
def onUploadSuccess(self, filePath, jid, url): #convenience method to detect file/image attributes for sending, requires existence of 'pillow' library logging.info("Upload success") entity = OutgoingChatstateProtocolEntity( ChatstateProtocolEntity.STATE_TYPING, jid) self.toLower(entity) entity = ImageDownloadableMediaMessageProtocolEntity.fromFilePath( filePath, url, None, jid) self.toLower(entity)
def onMessage(self, messageProtocolEntity): if messageProtocolEntity.getType() == 'text': global online global lastCommandTime message = messageProtocolEntity.getBody() print("Mensaje: " + str(message)) self.toLower( messageProtocolEntity.ack()) # Set received (double v) if not online: time.sleep(random.uniform(0.5, 1)) self.toLower( PresenceProtocolEntity(name=name)) #Set name SarPi self.toLower(AvailablePresenceProtocolEntity()) #Set online if lastCommandTime == 0: lastCommandTime = time.time() backgroundOTmr = Thread(target=self.onlineTimer) backgroundOTmr.start() lastCommandTime = time.time() online = True random.uniform(0.5, 1.5) self.toLower( messageProtocolEntity.ack(True)) #Set read (double v blue) time.sleep(0.5) if message[0] == '.': self.toLower( OutgoingChatstateProtocolEntity( OutgoingChatstateProtocolEntity.STATE_TYPING, Jid.normalize(messageProtocolEntity.getFrom( False)))) #Set is writing time.sleep(random.uniform(0.5, 1.5)) self.toLower( OutgoingChatstateProtocolEntity( OutgoingChatstateProtocolEntity.STATE_PAUSED, Jid.normalize(messageProtocolEntity.getFrom( False)))) #Set no is writing self.onTextMessage(messageProtocolEntity) #Send the answer
def stop_typing(self, conversation): self.toLower(OutgoingChatstateProtocolEntity( OutgoingChatstateProtocolEntity.STATE_PAUSED, Jid.normalize(conversation) ))
def start_typing(self, conversation): self.toLower(OutgoingChatstateProtocolEntity( OutgoingChatstateProtocolEntity.STATE_TYPING, Jid.normalize(conversation) ))
def test_send(self): entity = OutgoingChatstateProtocolEntity( OutgoingChatstateProtocolEntity.STATE_PAUSED, "*****@*****.**") self.assertSent(entity)
def onMessage(self, messageProtocolEntity): #send receipt otherwise we keep receiving the same message over and over if messageProtocolEntity.getType() == 'text': #self.onTextMessage(messageProtocolEntity) msgFrom = messageProtocolEntity.getFrom() msgText = messageProtocolEntity.getBody().lower() msgType = messageProtocolEntity.getType() jid = self.normalizeJid(msgFrom) textMsg = ' ' modwiki = ' ' if True: #msgText = msgText.encode('utf8') if msgText == 'hi': logging.info('Hii from rk') textMsg = """ [*AutoBot*] _Hii.. Im AutoBot,Please try below commands_ *Hi* -Try this! *wiki eagle* -Get result from Wikipedia *wiki set-lang ta* -Set language eg: ta=tamil,en=english *Amazon Iphone* -Get results from Amazon *Rk* -Try this ! *Kabali* -Try this! */help* -Show this message *exit!* -Killing AutoBot """ elif msgText == 'rk': jid = self.normalizeJid(msgFrom) entity = OutgoingChatstateProtocolEntity( ChatstateProtocolEntity.STATE_TYPING, jid) self.toLower(entity) textMsg = 'Hello Boss.. :)' ''' elif msgText == 'wiki': textMsg = 'Wiki Details:' modwiki = 'Add a word after wiki to get details.Eg: wiki eagle' ''' elif 'wiki' in msgText: textMsg = 'Wiki Details:' #wikipedia.set_lang('ta') splitted = msgText.split() if msgText.split(' ')[1] == 'set-lang': try: wikipedia.set_lang(msgText.split(' ')[2]) modwiki = 'Wiki language Changed to ' + msgText.split( ' ')[2] except: modwiki = 'Unable to Set Language' elif msgText.split(' ')[1] != 'set-lang': #modwiki = wikipedia.summary(msgText.split(' ',1)[1]).encode('utf-8')#encoding to avoid unicode error jid = self.normalizeJid(msgFrom) entity = OutgoingChatstateProtocolEntity( ChatstateProtocolEntity.STATE_TYPING, jid) self.toLower(entity) logging.info('This is Wiki App') try: modwiki = wikipedia.summary( msgText.split(' ', 1)[1]).encode( 'utf-8') #encoding to avoid unicode error except ValueError: modwiki = 'Sorry value error Page not Found!..Please try with different search term' except wikipedia.exceptions.PageError: modwiki = 'Sorry Page not Found!..Please try with different search term' except wikipedia.exceptions.DisambiguationError as e: # print (e.options)# this will print modwiki = ', '.join(e.options).encode('utf-8') except: modwiki = 'Unknown Error Check with Rk' else: modwiki = 'Add a word after wiki to get details.Ex: wiki cool' elif msgText == 'kabali': #Generating random Img from Folder #jid = self.normalizeJid(msgFrom) random_filename = random.choice([ x for x in os.listdir(config.media_storage_path) if os.path.isfile( os.path.join(config.media_storage_path, x)) ]) path = config.media_storage_path + '/' + random_filename print path textMsg = 'Uploading kabali Image....' iqEntity = RequestUploadIqProtocolEntity( RequestUploadIqProtocolEntity.MEDIA_TYPE_IMAGE, filePath=path) uploadOk = lambda successEntity, originalEntity: \ self.onRequestUploadResult(successEntity, originalEntity, jid, path, self.onUploadProgress) self._sendIq(iqEntity, uploadOk, self.onRequestUploadError) self.send elif 'amazon' in msgText: textMsg = 'Amazon Details:' if len(msgText.split()) > 1: modwiki = (scp.search(msgText.split(' ', 1)[1])) os.remove('myfile.txt') else: modwiki = 'Add a word after Amazon to get details.Ex: Amazon bag' elif msgText == '/help': logging.info('Sending Help Msg..') textMsg = """ [HELP] - Commands */help* - Show this message. *Hi* - Try this! *wiki eagle* - Gets Result from Wikipedia for search 'eagle' *wiki set-lang ta* - Set Search language for wiki eg: ta=tamil ,en=english *Amazon Iphone* - Fetched all displayed Prize and details for product 'Iphone' *Rk* - Try this ! *Kabali* - Just Try typing 'Kabali' and see for yourself!! *exit!* - killing AutoBot """ elif messageProtocolEntity.getFrom( False ) == 'enteradmin no,eg:919884098840' and msgText == 'exit!': modwiki = 'kiiling bot..' os.kill(ypid, 9) else: logging.info('Auto Reply Disabled') msgFrom = 'nomsg' if msgFrom != 'nomsg': outgoingMessageProtocolEntity = TextMessageProtocolEntity( textMsg + " " + modwiki, to=msgFrom) self.toLower(outgoingMessageProtocolEntity) logging.info( "Message:%s|From:%s|Time:%s|" % (msgFrom, messageProtocolEntity.getFrom(False), time.ctime())) elif messageProtocolEntity.getType() == 'media': logging.warning("Media received") try: outgoingMessageProtocolEntity = TextMessageProtocolEntity( 'Media Not Suppported' + " " + time.ctime(), to=messageProtocolEntity.getFrom()) self.toLower(outgoingMessageProtocolEntity) except ValueError: logging.exception('audio not supported') elif message.getMediaType() == 'ptt': logging.info("Audio received") outgoingMessageProtocolEntity = TextMessageProtocolEntity( 'Audio Not Suppported' + " " + time.ctime(), to=messageProtocolEntity.getFrom()) self.toLower(outgoingMessageProtocolEntity) elif messageProtocolEntity.getType() == 'vcard': logging.info("vcard received") outgoingMessageProtocolEntity = TextMessageProtocolEntity( 'vcard Not Suppported' + " " + time.ctime(), to=messageProtocolEntity.getFrom()) self.toLower(outgoingMessageProtocolEntity) #send delivery receipt with time self.toLower(messageProtocolEntity.ack(True))
def stop_typing(self, num): self.toLower( OutgoingChatstateProtocolEntity( OutgoingChatstateProtocolEntity.STATE_PAUSED, num))
def start_typing(self, from_): self.toLower( OutgoingChatstateProtocolEntity( OutgoingChatstateProtocolEntity.STATE_TYPING, from_))