-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_api.py
114 lines (97 loc) · 4.76 KB
/
aws_api.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
import boto3
from botocore.exceptions import ClientError
from datetime import datetime
class amazonApi:
def __init__(self):
self.pinpointClient = boto3.client('pinpoint')
self.cognitoClient = boto3.client('cognito-idp')
self.awsCogntioId = 'test'
self.awsAppId = 'test'
self.senderId = 'TESTING_USER'
def sendSMSCode(self,mobile, code):
try:
response = self.pinpointClient.send_messages(ApplicationId=self.awsAppId, MessageRequest={
'Addresses':{
mobile:{'ChannelType':'SMS'}},
'MessageConfiguration':{
'SMSMessage':{'Body':code,'MessageType':'TRANSACTIONAL','SenderId':self.senderId}}
})
except ClientError as e:
print('Error: ' + e.response['Error']['Message'])
return e.response['Error']['Message']
else:
print("Message sent!")
return "Message sent!"
def sendEmailCode(self,email, code, location):
emailTemplate = self.pinpointClient.get_email_template(TemplateName='Send_SMS_Code', Version='latest')
try:
response = self.pinpointClient.send_messages(ApplicationId=self.awsAppId, MessageRequest={
'Addresses':{
email:{'ChannelType':'EMAIL'}},
'MessageConfiguration':{
'EmailMessage':{'FromAddress':'test@gmail.com',
'SimpleEmail':{
'HtmlPart':{
'Data': self.getEmailTemplate(emailTemplate['EmailTemplateResponse']['HtmlPart'],email,code,location)
},
'Subject':{
'Data':emailTemplate['EmailTemplateResponse']['Subject']
}}}}})
except ClientError as e:
print(e.response['Error']['Message'])
return e.response['Error']['Message']
else:
print("Message sent!")
return "Message sent!"
def getEmailTemplate(self, template, email, code, location):
template.replace("{User.Email}", email)
template.replace("{User.Code}", code)
now = datetime.now()
template.replace("{App.Timezone}",now.strftime("%d/%m/%Y %H:%M:%S"))
template.replace("{App.Location}", location);
template.replace("{App.Name}", 'sadasd')
return template
def createUserIdentity(self, userName, nickName, email=None, mobile=None):
attribute = [{'Name':'nickname', 'Value' : nickName}]
if email != None:
data = [{'Name':'email' , 'Value' : email}]
userName = email
elif mobile != None:
data = [{'Name' : 'phone_number' , 'Value' : mobile}]
userName = mobile
try:
response = self.cognitoClient.admin_create_user(UserPoolId = self.awsCogntioId,
Username = userName,
UserAttributes = attribute,
ValidationData = data,
TemporaryPassword ='Abcd1234',
MessageAction = 'SUPPRESS',
ForceAliasCreation = True)
except ClientError as e:
print(e.response['Error']['Message'])
return (e.response['Error']['Message'])
else:
print("Message sent!")
return True
def updateUserIdentity(self, userName, nickName, password, email=None, mobile=None):
attribute = [{'Name':'nickname', 'Value' : nickName}]
if email != None:
data = [{'Name':'email' , 'Value' : email}]
userName = email
elif mobile != None:
data = [{'Name' : 'phone_number' , 'Value' : mobile}]
userName = mobile
try:
response = self.cognitoClient.admin_update_user_attributes(UserPoolId = self.awsCogntioId,
Username = userName,
UserAttributes = attribute)
response = self.cognitoClient.admin_set_user_password(UserPoolId = self.awsCogntioId,
Username = userName,
Password = password,
Permanent = True)
except ClientError as e:
print(e.response['Error']['Message'])
return False
else:
print("Message sent!")
return response