Skip to content

Commit f438de5

Browse files
committed
Add whatsapp bot to individual project
1 parent bcba253 commit f438de5

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# This is a whatsapp bot that uses Cricket API to fetch different types of match data including lives ones, past and upcoming ones and send them on your whatsapp number. Too easy to use. Simply put your credentials from Twilio and Cric into the place, install twilio api and you will be ready to go.
2+
from twilio.rest import Client
3+
import requests
4+
import datetime
5+
import time
6+
7+
# Your Account SID from twilio.com/console
8+
account_sid = "YouSID"
9+
# Your Auth Token from twilio.com/console
10+
auth_token = "YouAuthToken"
11+
12+
client = Client(account_sid, auth_token)
13+
#using the API to get match details from recently happend and going to happen matches
14+
resp=requests.get("https://cricapi.com/api/matches?apikey=YourAPIKEY");
15+
data=resp.json()["matches"]
16+
17+
#Returns filtered matches on the basis of the team whose match you want
18+
def return_all_match(teamName,data):
19+
"""
20+
teamName: any cricket team name
21+
data: the data that we get from the API call
22+
"""
23+
allMatch=[]
24+
for each in data:
25+
if (teamName in each["team-1"]) or (teamName in each["team-2"]):
26+
allMatch.append(each)
27+
return allMatch
28+
29+
result=return_all_match("India",data) #storing list of matches in which either team was "India"
30+
31+
#Filters out the matches on the basis of current date
32+
def filter_date(data):
33+
"""
34+
data: a list of all the matches to filter by date
35+
"""
36+
current_date=str(datetime.datetime.now())
37+
current_date=current_date[:current_date.index(' ')]
38+
results=[]
39+
for each in data:
40+
if current_date in each["date"]:
41+
results.append(each)
42+
return results
43+
44+
result2=filter_date(result) #Filtered the result list down to by only returning matches with todays date
45+
46+
#return detailed information about the match(single) that you pass
47+
def return_score(id):
48+
"""
49+
id: you pass the "unique_id" of the match who's info you want
50+
"""
51+
id=str(id)
52+
response=requests.get("https://cricapi.com/api/cricketScore?apikey=YourAPIKEY&unique_id="+id)
53+
if response.status_code!=200:
54+
return 0
55+
else:
56+
return response.json()
57+
58+
#Returns detailed information about the matches through return_score() function
59+
def return_detailed_info(matches):
60+
"""
61+
matches: list of the matches
62+
"""
63+
allPresent=[]
64+
for each in matches:
65+
allPresent.append(return_score(each["unique_id"]))
66+
return allPresent
67+
68+
print("-"*20)
69+
print("Starting AP BOT")
70+
allPresent=return_detailed_info(result2) #list of matches with detailed info of every match
71+
72+
#Converts JSON into a better readable and good looking format
73+
def readable(data):
74+
"""
75+
data: Single JSON value from the API
76+
"""
77+
text=""
78+
keys=["stat","score","team-1","team-2","v","ttl","matchStarted","dateTimeGMT","type"]
79+
for elements in keys:
80+
if elements in data.keys():
81+
text+="*"+elements+"* "+str(data[elements])+"\n"
82+
return text
83+
84+
85+
if __name__=="__main__":
86+
#The message sending part
87+
counter=0 #Just to bound check
88+
#allPresent sotres the final list of matches
89+
for each in allPresent:
90+
to_send=readable(each)
91+
message = client.messages.create(
92+
to="whatsapp:+910000000000", #replace zeros with sender's number
93+
from_="whatsapp:+1000000000", #Replace zeros with twilio's number
94+
body=to_send)
95+
time.sleep(2)
96+
print(message.sid)
97+
if counter>15:
98+
#Security check loop , so in case somehow anything make list big enough it don't kill their server
99+
break
100+
print("Process Finished","\nTotal messages send = ",counter)

0 commit comments

Comments
 (0)