Skip to content

Commit a545178

Browse files
committed
Added WhatsappBot
Whatsapp bot app is capable of reading messages from the specified user or group and send reply based on the condition given in CSV
1 parent 9976472 commit a545178

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

Scripts/whatsappbot.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from selenium import webdriver
2+
from selenium.webdriver.chrome.options import Options
3+
from selenium.webdriver.common.keys import Keys
4+
from selenium.webdriver.common.action_chains import ActionChains
5+
from requests import get
6+
from bs4 import BeautifulSoup as bs
7+
import keyboard
8+
import time
9+
import click
10+
import os
11+
import sys
12+
import csv
13+
import threading
14+
15+
chrome_options = Options()
16+
chrome_options.add_argument(
17+
"user-data-dir=" + os.path.dirname(sys.argv[0]))
18+
driver = webdriver.Chrome(chrome_options=chrome_options)
19+
driver.maximize_window()
20+
21+
driver.get("https://web.whatsapp.com")
22+
# Time to load the QR Code and scenning
23+
time.sleep(25)
24+
25+
# Key in the value of the Chat name that you want to read the messages and reply
26+
target = '"your_friend/group_name"'
27+
28+
# Identify the Chatlist based on its element
29+
panel = driver.find_element_by_class_name('chatlist-panel-body')
30+
31+
elem = None
32+
a = 0
33+
while elem is None:
34+
a += 300
35+
try:
36+
driver.execute_script('arguments[0].scrollTop = %s' % a, panel)
37+
elem = driver.find_element_by_xpath(
38+
'//span[@title=' + target + ']')
39+
except:
40+
pass
41+
42+
ac = ActionChains(driver)
43+
ac.move_to_element(elem).click().perform()
44+
time.sleep(2)
45+
46+
url = driver.page_source
47+
48+
def readMessage():
49+
threading.Timer(5.0, readMessage).start()
50+
url = driver.page_source
51+
soup = bs(url, "lxml")
52+
53+
try:
54+
gotdiv = soup.find_all("div", { "class" : "msg msg-group" })[-1]
55+
except IndexError:
56+
gotdiv = 'null'
57+
58+
if gotdiv == 'null':
59+
div = soup.find_all("div", { "class" : "bubble bubble-text copyable-text" })[-1]
60+
# print(div)
61+
else:
62+
div = soup.find_all("div", { "class" : "msg msg-group" })[-1]
63+
64+
text = div.find_all('span')
65+
print(text)
66+
67+
try:
68+
gottext = text[4].find_all(text=True)[1]
69+
except IndexError:
70+
gottext = 'null'
71+
72+
if gottext == 'null':
73+
div = soup.find_all("div", { "class" : "chat-title" })[-1]
74+
name = div.find_all(text=True)[1]
75+
try:
76+
msg = text[-2].find_all(text=True)[1].lower()
77+
except IndexError:
78+
msg = "You replied last"
79+
time = text[-1].find(text=True)
80+
81+
else: #group
82+
name = text[3].find_all(text=True)[1]
83+
try:
84+
msg = text[4].find_all(text=True)[1].lower()
85+
except IndexError:
86+
msg = "You replied last"
87+
try:
88+
time = text[-2].find(text=True)
89+
except:
90+
time = "None"
91+
92+
93+
print(name, msg, time)
94+
95+
# Getting appropriate reply from the csv
96+
# Bot will lookup the csv for reply Only if the text contains the word buddy
97+
98+
if "buddy" in msg:
99+
100+
with open('dict.csv', "r") as f:
101+
reader = csv.reader(f)
102+
chat = {}
103+
104+
for row in reader:
105+
key = row[0]
106+
chat[key] = row[1:]
107+
try:
108+
gotreply = chat[msg]
109+
except KeyError:
110+
gotreply = 'null'
111+
112+
print(gotreply)

0 commit comments

Comments
 (0)