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

2024-05-20 02:04:44 发布

您现在位置:Python中文网/ 问答频道 /正文

在为CustomMessage_AdminCreateUser触发器发送自定义电子邮件时,我成功地更改了从Amazon Cognito接收的事件中的emailSubject属性,但似乎无法更改emailMessage属性。在

从Cognito发送的电子邮件包含正确的自定义主题,但消息根本不是自定义的,它始终是在Cognito池设置中设置的主题。在

处理从Cognito接收到的事件的lambda处理程序成功地自定义了以下触发器的消息:

  • CustomMessage_SignUp
  • CustomMessage_ResendCode
  • CustomMessage_ForgotPassword

但对于CustomMessage_AdminCreateUser触发器,我似乎无法让它工作(至少不能完全工作)。在

我尝试将email_verified用户属性设置为true,以查看该属性是否依赖于将自定义邮件成功发送给创建的用户。另外,我尝试在Docker容器中运行lambda,以查看返回到Cognito的最终事件的输出,但是事件包含正确的数据,email subject和email message都是定制的。在

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
^{pr2}$
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)

Tags: selfeventmessagegetreturn属性emailresponse
3条回答

我也有同样的问题。在

我的初始自定义消息:

<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.codeParameter和{}(因此在我的初始代码案例中,不是event.userName)。在

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

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

^{pr2}$

对于链接,您可以使用event.userName-这应该是cognito用户的子节点。只需确保在电子邮件的其他地方包含event.request.usernameParameter。在我的例子中,我用Welcome, ${event.request.usernameParameter}!开始邮件

对于CustomMessage_AdminCreateUser触发器,一件重要的事情是必须在邮件emailMessage中包含username和{}。在

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)

{{cd6>

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

相关问题 更多 >