forked from mukulhase/WebWhatsapp-Wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWhatsappAPI.py
51 lines (41 loc) · 1.47 KB
/
WhatsappAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python2
import time
from webwhatsapp import WhatsAPIDriver
import re
from pprint import pprint
class WhatsappAPI(object):
__driver = None
__ultimasMensagens = {}
__tempoEspera = 10
def __init__(self):
self.__driver = WhatsAPIDriver()
self.__driver.firstrun()
self.espera()
self.__driver.view_unread()
def setTempoEspera(self, tempo):
self.__tempoEspera = tempo
def espera(self):
time.sleep(self.__tempoEspera)
def getMensagens(self):
self.espera()
javaScript = self.__driver.view_unread()
contatos = {}
contato = {}
for contato in javaScript:
jid = contato[u'id']
contato['contato'] = contato[u'contact']
self.salvaUltimaMensagem(jid, contato[u'messages'][0][u'id'])
contato['mensagens'] = self.ordenaMsg(contato[u'messages'])
contatos[jid] = contato.copy()
contato.clear()
return contatos
def ordenaMsg(self, mensagens):
return mensagens.reverse()
def filtraJid(self, jid):
return " ".join(re.findall("[0-9]+", jid))
def salvaUltimaMensagem(self, jid, idMensagem):
self.__driver.set_ultima_msg_recebida(jid, idMensagem)
#self.__ultimasMensagens[jid] = idMensagem
def enviarMensagem(self, jid, mensagem):
print jid
self.__driver.send_to_whatsapp_id(jid, mensagem)