-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
127 lines (106 loc) · 4.58 KB
/
application.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
import email
import imaplib
import threading
import time
import pymysql
from twilio.twiml.messaging_response import MessagingResponse
from flask import Flask,request
from twilio.rest import Client
account_sid = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
twilio_client = Client(account_sid, auth_token)
connection = {
"host":'database-x-xxx-x.rds.amazonaws.com',
'username':'XXXX',
'password':'XXXX',
'db':'DbName'
}
conn = pymysql.connect(connection['host'],connection['username'],connection['password'],connection['db'])
cursor = conn.cursor()
def get_text(msg):
if msg.is_multipart():
return get_text(msg.get_payload(0))
else:
return msg.get_payload(None, True)
application = Flask(__name__)
@application.route('/')
def index():
return "Hello World"
@application.route('/sms',methods=["POST"])
def sms_reply():
contact_Number = request.form['From']
resp = MessagingResponse()
count_statement = "select count(*) from WhatsAppSubscribers where phoneNo = '{}'".format(contact_Number)
try:
cursor.execute(count_statement)
result = cursor.fetchall()[0][0]
print(result)
if result == 0:
insert_statement = "Insert into WhatsAppSubscribers(phoneNo) values('{}')".format(contact_Number)
print(insert_statement)
try:
cursor.execute(insert_statement)
resp.message("Thank you for your interest. Now, You can sit back and Relax. \n\n All the mails from the"
+ " Placement department will be there straight in your WhatsApp.\n\n Please note that, there might be a delay of 2 minutes from the"
+" time mail is sent.\n\n Also, though we try our best, to help you but there can some errors.\n\n So, Please check your emails regularly.\n\n Thank you again for Subscribing. "+"\U0001f600")
conn.commit()
except:
print("hi")
resp.message("Sorry, Some error occured. We hope to server you in future.")
else:
resp.message("Hello, You are already Subscribed. You will receive the updated timely \U0001f600. ")
except:
resp.message("Sorry, Some error occured. We hope to server you in future.")
return str(resp)
#User Credentials.
userName = "[email protected]"
password = "XXXXXXXXXXXXXXX"
#Performing login
m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login(userName,password)
class MailThread(threading.Thread):
def __init__(self,var):
threading.Thread.__init__(self)
self.last_email_uid = var
def run(self):
while True:
m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login(userName,password)
m.select("PlacementMail")
result, data = m.uid('search', None, "ALL")
all_email_uids = data[0].split()
print(all_email_uids)
if result == 'OK':
last_email = int(data[0].split()[-1].decode("utf-8"))
for num in range(self.last_email_uid+1,last_email+1):
a = str(num)
res = bytes(a, 'utf-8')
result, data = m.uid('fetch',res, '(RFC822)')
if result == 'OK':
email_message = email.message_from_bytes(data[0][1])
email_subject = email_message['Subject']
email_body = get_text(email_message).decode("utf-8")
body = "Subject: *{}* \n\n *We are currently not sending the mail attachments.* \n\n Content: {} \n\n".format(email_subject,email_body)
body = body[0:1600]
count_statement = "select * from WhatsAppSubscribers"
try:
cursor.execute(count_statement)
result = cursor.fetchall()
except:
print("hi")
clients = [i[1] for i in result]
for client in clients:
print(client)
twilio_client.messages.create(
body=body,
from_='whatsapp:+14155238886',
to= client
)
time.sleep(30)
self.last_email_uid = num
m.close()
m.logout()
if __name__ == "__main__":
mailThread = MailThread(1)
mailThread.start()
application.run()