Jos*_*rić 3 python amazon-web-services amazon-cognito amazon-cognito-triggers
发送用于触发器的自定义电子邮件时CustomMessage_AdminCreateUser
,我成功更改了emailSubject
从 Amazon Cognito 收到的事件中的属性,但似乎无法更改该emailMessage
属性。
从 Cognito 发送的电子邮件包含正确的自定义主题,但该消息根本没有自定义,并且始终是 Cognito 池设置中设置的主题。
处理从 Cognito 接收到的事件的 lambda 处理程序成功地为这些触发器自定义了消息:
CustomMessage_SignUp
CustomMessage_ResendCode
CustomMessage_ForgotPassword
但我似乎无法让它工作(至少不能完全)CustomMessage_AdminCreateUser
触发。
我尝试将email_verified
用户属性设置为true
,以查看该属性是否取决于成功将自定义邮件发送到创建的用户。另外,我尝试在 Docker 容器中运行 lambda 以查看返回到 Cognito 的最终事件的输出,但该事件包含正确的数据,电子邮件主题和电子邮件消息都是自定义的。
def lambda_handler(event, context):
if event['userPoolId'] == os.getenv('cognitoPoolId'):
if CustomMessageTriggerEnum.has_value(event.get('triggerSource')):
custom_message_trigger = CustomMessageTriggerEnum(event.get('triggerSource'))
if custom_message_trigger == CustomMessageTriggerEnum.CustomMessageAdminCreateUser:
custom_message_trigger = CustomMessageAdminCreateUser(event)
else:
return None
custom_response = custom_message_trigger.get_custom_response(
custom_message_trigger.ACTION,
custom_message_trigger.EMAIL_SUBJECT,
custom_message_trigger.EMAIL_MESSAGE
)
event = custom_message_trigger.set_custom_response(**custom_response)
return event
Run Code Online (Sandbox Code Playgroud)
class CustomMessageAdminCreateUser(BaseCustomMessageTrigger):
""" Custom message admin create user trigger """
ACTION = "changepassword"
EMAIL_SUBJECT = "Welcome to {service}"
EMAIL_MESSAGE = "Your account has been created. <a href='{0}'>Click here</a> to set your password and start using {service}."
def __init__(self, event):
super().__init__(event)
Run Code Online (Sandbox Code Playgroud)
class BaseCustomMessageTrigger():
""" Base custom message trigger """
def __init__(self, event):
self.event = event
def get_custom_response(self, action, email_subject, email_message):
""" Gets custom response params as dictionary """
request = self.event.get('request')
custom_response = {}
url = self.get_url(
action=action,
code=request.get('codeParameter'),
email=urlencode({'email': request['userAttributes'].get('email')})
)
custom_response['emailSubject'] = email_subject
custom_response['emailMessage'] = email_message.format(url)
return custom_response
def set_custom_response(self, **kwargs):
""" Updates the event response with provided kwargs """
response = self.event.get('response')
response.update(**kwargs)
return self.event
def get_url(self, action, code, email):
""" Used for constructing URLs. """
rawUrl = 'https://{0}/{1}?code={2}&{3}'
return rawUrl.format(domain, action, code, email)
Run Code Online (Sandbox Code Playgroud)
只是为了澄清电子邮件实际上应包含event.request.usernameParameter
和中的值event.request.codeParameter
中的值。
我发现 Cognito 忽略了我的电子邮件正文,因为令牌{username}
虽然{####}
存在,但丢失了。
归档时间: |
|
查看次数: |
8136 次 |
最近记录: |