-
Notifications
You must be signed in to change notification settings - Fork 35
/
bot.py
129 lines (103 loc) · 4.47 KB
/
bot.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import time
import re
import requests
import json
from chatterbot.trainers import ListTrainer
from chatterbot import ChatBot
from selenium import webdriver
class wppbot:
dir_path = os.getcwd()
def __init__(self, nome_bot):
print(self.dir_path)
self.bot = ChatBot(nome_bot)
self.bot.set_trainer(ListTrainer)
self.chrome = self.dir_path+'\chromedriver.exe'
self.options = webdriver.ChromeOptions()
self.options.add_argument(r"user-data-dir="+self.dir_path+"\profile\wpp")
self.driver = webdriver.Chrome(self.chrome, chrome_options=self.options)
def inicia(self,nome_contato):
self.driver.get('https://web.whatsapp.com/')
self.driver.implicitly_wait(15)
self.caixa_de_pesquisa = self.driver.find_element_by_class_name('jN-F5')
self.caixa_de_pesquisa.send_keys(nome_contato)
time.sleep(2)
print(nome_contato)
self.contato = self.driver.find_element_by_xpath('//span[@title = "{}"]'.format(nome_contato))
self.contato.click()
time.sleep(2)
def saudacao(self,frase_inicial):
self.caixa_de_mensagem = self.driver.find_element_by_class_name('_2S1VP')
if type(frase_inicial) == list:
for frase in frase_inicial:
self.caixa_de_mensagem.send_keys(frase)
time.sleep(1)
self.botao_enviar = self.driver.find_element_by_class_name('_35EW6')
self.botao_enviar.click()
time.sleep(1)
else:
return False
def escuta(self):
post = self.driver.find_elements_by_class_name('_3_7SH')
ultimo = len(post) - 1
texto = post[ultimo].find_element_by_css_selector('span.selectable-text').text
return texto
def aprender(self,ultimo_texto,frase_inicial,frase_final,frase_erro):
self.caixa_de_mensagem = self.driver.find_element_by_class_name('_2S1VP')
self.caixa_de_mensagem.send_keys(frase_inicial)
time.sleep(1)
self.botao_enviar = self.driver.find_element_by_class_name('_35EW6')
self.botao_enviar.click()
self.x = True
while self.x == True:
texto = self.escuta()
if texto != ultimo_texto and re.match(r'^::', texto):
if texto.find('?') != -1:
ultimo_texto = texto
texto = texto.replace('::', '')
texto = texto.lower()
texto = texto.replace('?', '?*')
texto = texto.split('*')
novo = []
for elemento in texto:
elemento = elemento.strip()
novo.append(elemento)
self.bot.train(novo)
self.caixa_de_mensagem.send_keys(frase_final)
time.sleep(1)
self.botao_enviar = self.driver.find_element_by_class_name('_35EW6')
self.botao_enviar.click()
self.x = False
return ultimo_texto
else:
self.caixa_de_mensagem.send_keys(frase_erro)
time.sleep(1)
self.botao_enviar = self.driver.find_element_by_class_name('_35EW6')
self.botao_enviar.click()
self.x = False
return ultimo_texto
else:
ultimo_texto = texto
def noticias(self):
req = requests.get('https://newsapi.org/v2/top-headlines?sources=globo&pageSize=5&apiKey=f6fdb7cb0f2a497d92dbe719a29b197f')
noticias = json.loads(req.text)
for news in noticias['articles']:
titulo = news['title']
link = news['url']
new = 'bot: ' + titulo + ' ' + link + '\n'
self.caixa_de_mensagem.send_keys(new)
time.sleep(1)
def responde(self,texto):
response = self.bot.get_response(texto)
# if float(response.confidence) > 0.5:
response = str(response)
response = 'bot: ' + response
self.caixa_de_mensagem = self.driver.find_element_by_class_name('_2S1VP')
self.caixa_de_mensagem.send_keys(response)
time.sleep(1)
self.botao_enviar = self.driver.find_element_by_class_name('_35EW6')
self.botao_enviar.click()
def treina(self,nome_pasta):
for treino in os.listdir(nome_pasta):
conversas = open(nome_pasta+'/'+treino, 'r').readlines()
self.bot.train(conversas)