如何为CustomMessage_AdminCreateUser触发器发送自定义消息?

时间:2019-07-03 07:38:21

标签: python amazon-cognito

在为CustomMessage_AdminCreateUser触发器发送自定义电子邮件时,在从Amazon Cognito收到的事件中,我成功更改了emailSubject属性,但似乎无法更改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
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)
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)

4 个答案:

答案 0 :(得分:3)

您必须避免在响应参数上使用urlencode,因为Lambda将添加占位符,稍后将由Cognito替换。

答案 1 :(得分:2)

只是为了澄清电子邮件,字面上应包含event.request.usernameParameterevent.request.codeParameter中的值。

我发现Cognito忽略了我的电子邮件正文,因为即使有{username}也缺少令牌{####}

答案 2 :(得分:0)

CustomMessage_AdminCreateUser触发的一件重要事情是,您必须在邮件username中包含passwordemailMessage

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)

请通过domain方法传递get_url

答案 3 :(得分:0)

我遇到了完全相同的问题。

我的初始自定义消息:

<html>
    <body>
      <p> Welcome. Follow the link below to unlock your account and set a new password.</p>
      <a href="http://localhost:3000/setNewPasswordcode_parameter=${event.request.codeParameter}&user_name=${event.userName}">Set Password</a>
    </body>
</html>

问题是您需要在模板中同时使用event.request.codeParameterevent.request.usernameParameter(因此在我的初始代码中不是event.userName)。

此外,codeParameter和usernameParameter必须仅包含URI有效字符。因此,如果用户名是包含“ @”符号的电子邮件地址,则不能在链接中使用它。

解决方法:对于临时密码,可以通过传入TemporaryPassword参数来确保在调用adminCreateUser()时仅使用URI有效字符。


cognitoIdentityServiceProvider.adminCreateUser({ 
  UserPoolId,
  Username, 
  TemporaryPassword
}).promise()

对于链接,您可以使用event.userName-这应该是Cognito用户的子链接。只要确保在电子邮件中的其他位置添加event.request.usernameParameter即可。就我而言,我以Welcome, ${event.request.usernameParameter}!

开始邮件