<@U04UQ68083H> how to trigger send verification to...
# ory-copilot
n
@chilly-king-10285 how to trigger send verification to only phone, because email is already verified
b
A message from kapa.ai
To trigger sending verification to only the phone number when the email is already verified, you need to configure your Ory Kratos setup correctly. Here's how you can achieve this: 1. First, ensure that your identity schema includes both email and phone number traits, with the phone number marked for verification via SMS. Here's an example of how to structure this in your identity schema:
Copy code
json {   "properties": {     "traits": {       "type": "object",       "properties": {         "email": {           "type": "string",           "format": "email",           "<http://ory.sh/kratos|ory.sh/kratos>": {             "verification": {               "via": "email"             }           }         },         "phone": {           "type": "string",           "format": "tel",           "<http://ory.sh/kratos|ory.sh/kratos>": {             "verification": {               "via": "sms"             }           }         }       }     }   } }
[Source](https://www.ory.sh/docs/kratos/self-service/flows/verify-email-account-activation#phone-number-verification) 2. Configure the SMS channel for sending verification codes. You'll need to set up an SMS courier in your Ory configuration. Refer to the [SMS documentation](https://www.ory.sh/docs/kratos/emails-sms/sending-sms) for detailed instructions on how to do this. 3. When triggering the verification flow, you can specify the method as "code" and provide only the phone number. This way, Kratos will send a verification code to the phone number without attempting to verify the email again. Here's an example of how you might structure the API request:
Copy code
json {   "method": "code",   "code": "PHONE_NUMBER_HERE" }
[Source](https://www.ory.sh/docs/reference/api) By following these steps, you can set up your Ory Kratos instance to send verification only to the phone number when the email is already verified. Remember to properly configure your SMS templates and courier settings to ensure the verification messages are sent correctly.
n
@chilly-king-10285 I want to send sms with request body
Copy code
function(ctx) {
  To: ctx.recipient
  Code: <verification code>
}
what is the correct request body for <verification code>
b
A message from kapa.ai
To send an SMS with the request body you've specified, you need to use the correct template variable for the verification code. Based on the information provided in the knowledge sources, here's how you can correctly format the request body:
Copy code
jsonnet function(ctx) {   To: ctx.recipient,   Code: ctx.template_data.VerificationCode }
The correct way to access the verification code is through
ctx.template_data.VerificationCode
. This is because the
template_data
object contains the specific data for the template, including the verification code. Here's why this is the correct approach: 1. The
ctx
object provides several fields, including
template_data
which contains template-specific information [Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#body-configuration). 2. The
template_data
field includes the
VerificationCode
for verification templates [Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#body-configuration). 3. The SMS template uses Jsonnet for configuration, allowing you to access these context variables [Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#body-configuration). Remember to configure your SMS courier properly in your Ory configuration to use this template. You may need to encode this Jsonnet function as a base64 string when setting it up in your configuration file or through the Ory CLI [Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#templates).
n
@chilly-king-10285 got
Message was abandoned because it did not deliver after 6 attempts
when use
Copy code
function(ctx) {
  To: ctx.recipient,
  Code: ctx.template_data.VerificationCode
}
b
A message from kapa.ai
I understand you're encountering an issue with message delivery when using the SMS template. Let's address this problem based on the information provided in the knowledge sources. The error message you're seeing, "Message was abandoned because it did not deliver after 6 attempts," indicates that the courier service tried to send the message multiple times but failed. This is related to the retry mechanism of Ory Kratos. Let's look at the possible reasons and solutions: 1. Retry Configuration: By default, Ory Kratos is set to retry message delivery 5 times. The error message you're seeing suggests that it attempted 6 times, which is slightly different from the default configuration. You can adjust this setting in your Kratos configuration file:
Copy code
yaml    courier:      message_retries: 5
[Source](https://www.ory.sh/docs/kratos/self-hosted/mail-courier-selfhosted#error-handling) 2. Template Format: The template you're using looks correct in terms of accessing the verification code. However, make sure it's properly formatted as a Jsonnet template. Here's a slight modification to ensure it's valid Jsonnet:
Copy code
jsonnet    function(ctx) {      To: ctx.recipient,      Code: ctx.template_data.VerificationCode    }
[Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#body-configuration) 3. SMS Gateway Configuration: Ensure that your SMS gateway is properly configured. The error might be occurring because the SMS gateway is not responding or is improperly set up. Double-check your SMS courier configuration in the Ory Kratos settings. [Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#templates) 4. Message Status: When a message fails to deliver after the specified number of retries, it gets the "Abandoned" status. You can check the status of messages using the Ory Kratos API. [Source](https://www.ory.sh/docs/reference/api) To troubleshoot this issue: 1. Verify your SMS gateway configuration. 2. Check the Kratos logs for more detailed error messages about why the delivery is failing. 3. Ensure that the phone number (recipient) is in the correct format. 4. If possible, try sending a test message through your SMS gateway directly to isolate whether the issue is with Kratos or the SMS service. If the problem persists after checking these points, you may need to investigate further by examining the Kratos logs or contacting Ory support for more detailed assistance.
n
@chilly-king-10285 i suspect you use wrong on
ctx.template_data.VerificationCode
when i use
ctx.body
, it success
b
A message from kapa.ai
You're absolutely right, and I apologize for the confusion in my previous answer. Thank you for pointing this out. Let's correct this based on the accurate information from the knowledge sources. You're correct that
ctx.body
is the proper way to access the message body in this context. The successful template you're using should look like this:
Copy code
jsonnet function(ctx) {   To: ctx.recipient,   Body: ctx.body }
This is indeed the correct format according to the Ory documentation [Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#body-configuration). The
ctx
object provides the following fields: - `recipient`: The recipient's phone number - `body`: The message body - `template_type`: The template type (e.g., "verification_code") - `template_data`: The template data - `message_type`: The message type (e.g., "sms") I apologize for the earlier mistake. You were right to suspect an issue with
ctx.template_data.VerificationCode
. The
ctx.body
already contains the formatted message body, including the verification code, so there's no need to access
VerificationCode
separately. Thank you for bringing this to my attention. It's great that you've found the correct solution, and I'm glad it's working successfully now.
n
@chilly-king-10285 so what are valid fields inside
function(ctx)
?
b
A message from kapa.ai
Based on the knowledge sources provided, I can give you information about the valid fields inside the
function(ctx)
for SMS templates in Ory Kratos. The following fields are available within the
ctx
object: 1. `recipient`: The recipient's phone number 2. `body`: The message body 3. `template_type`: The template type (e.g., "verification_code") 4. `template_data`: The template data 5. `message_type`: The message type (e.g., "sms") [Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#body-configuration) It's important to note that the
body
field already contains the formatted message, including any verification codes or other template-specific content. For email templates, there are additional fields available in the
ctx.template_data
object, which vary depending on the type of template (e.g., recovery, verification, login). However, since your question was in the context of SMS templates, these additional fields may not be directly applicable to the SMS
function(ctx)
. [Source](https://www.ory.sh/docs/kratos/emails-sms/custom-email-templates#available-variables) Remember that when using these fields in your Jsonnet template, you should access them directly from the
ctx
object, as demonstrated in your successful example:
Copy code
jsonnet function(ctx) {   To: ctx.recipient,   Body: ctx.body }
This format ensures that you're using the correct fields as provided by Ory Kratos for SMS templates.
n
@chilly-king-10285 so how to get the Verification Code on jsonnet?
b
A message from kapa.ai
Based on the information provided in the knowledge sources and our previous conversation, I need to clarify that accessing the Verification Code directly in the Jsonnet template for SMS is not necessary or recommended. Here's why: 1. The
ctx.body
field already contains the fully formatted message, including the verification code. [Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#body-configuration) 2. The correct way to use the SMS template is:
Copy code
jsonnet function(ctx) {   To: ctx.recipient,   Body: ctx.body }
[Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#body-configuration) This means that you don't need to explicitly access the Verification Code in your Jsonnet template. The
ctx.body
already includes it. If you need to customize how the verification code appears in the SMS, you should do this by modifying the SMS template configuration in your Ory Kratos settings. For example:
Copy code
yaml courier:   templates:     verification_code:       valid:         sms:           body:             plaintext: "<base64://WW91ciB2ZXJpZmljYXRpb24gY29kZSBpczoge3sgLlZlcmlmaWNhdGlvbkNvZGUgfX0>="
[Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#templates) In this configuration, you can use
{{ .VerificationCode }}
to place the verification code within your message template. The actual code will be inserted when the message is generated, and the result will be available in
ctx.body
when your Jsonnet function is called. Remember, the Jsonnet function for SMS is primarily used to structure the message for sending, not for composing the content of the message itself.
n
@chilly-king-10285 but when use
{{ .VerificationCode }}
on sms body it decoded result has
\n
how to prevent that
b
A message from kapa.ai
I understand you're encountering an issue with unwanted newline characters (
\n
) in your SMS body when using
{{ .VerificationCode }}
. Let's address this problem based on the information from our knowledge sources. To prevent the
\n
character from appearing in your SMS body, you can use the
trim
function in your template. Here's how you can modify your SMS template configuration:
Copy code
yaml courier:   templates:     verification_code:       valid:         sms:           body:             plaintext: "<base64://WW91ciB2ZXJpZmljYXRpb24gY29kZSBpczoge3sgdHJpbSAuVmVyaWZpY2F0aW9uQ29kZSB9fQ>=="
[Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#templates) In this configuration,
{{ trim .VerificationCode }}
will remove any leading or trailing whitespace, including newline characters, from the verification code. Remember to encode your template to base64 when setting it in the configuration file. The decoded version of the above example would be:
Copy code
Your verification code is: {{ trim .VerificationCode }}
If you're still experiencing issues after applying this change, you might want to check if there are any newline characters in your base template before the verification code is inserted. Ensure your plaintext template is a single line without any line breaks. Also, keep in mind that when you're using the Jsonnet function to structure your SMS message, you don't need to handle the verification code directly as it's already included in `ctx.body`:
Copy code
jsonnet function(ctx) {   To: ctx.recipient,   Body: ctx.body }
[Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#body-configuration) This approach should help prevent unwanted newline characters in your SMS body.
n
@chilly-king-10285 so I cannot get the data inside
template_data
in jsonnet?
b
A message from kapa.ai
Based on the information provided in the knowledge sources, I need to clarify and correct my previous statements. You can indeed access the data inside
template_data
in your Jsonnet template for SMS. Let me explain: 1. The
ctx
object passed to the Jsonnet function includes a
template_data
field, which contains various template-specific data. [Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#body-configuration) 2. For SMS templates, you can access this data using
ctx.template_data
. The available fields depend on the type of template being used. Here's an example of how you might access data from
template_data
in your Jsonnet function:
Copy code
jsonnet function(ctx) {   To: ctx.recipient,   Body: ctx.body,   VerificationCode: if "template_data" in ctx && "VerificationCode" in ctx.template_data then ctx.template_data.VerificationCode else null }
[Source](https://www.ory.sh/docs/kratos/emails-sms/sending-emails-smtp#payload) This example shows how to safely access the
VerificationCode
from
template_data
if it exists. It's important to note that while you can access this data, for SMS templates, the
ctx.body
field already contains the formatted message including any codes or template-specific content. The ability to access
template_data
directly gives you more flexibility in customizing your message structure if needed. I apologize for any confusion in my previous responses. Thank you for your question, as it has helped clarify this important point about Jsonnet templates in Ory Kratos.
n
@chilly-king-10285 remove retry send sms
b
A message from kapa.ai
To remove or adjust the retry mechanism for sending SMS in Ory Kratos, you can modify the
message_retries
configuration in your Kratos configuration file. Here's how you can do it: 1. Locate your Kratos configuration file (usually named
kratos.yml
or similar). 2. Add or modify the
courier
section to set
message_retries
to 0:
Copy code
yaml courier:   message_retries: 0
[Source](https://www.ory.sh/docs/kratos/self-hosted/mail-courier-selfhosted#error-handling) By setting
message_retries
to 0, Kratos will attempt to send the SMS only once and will not retry if the first attempt fails. If you're using Ory Network (the cloud version), you might need to update this configuration using the Ory CLI:
Copy code
sh ory patch identity-config <your-project-id> \   --add '/courier/message_retries=0'
[Source](https://www.ory.sh/docs/guides/cli/identity-cli) Please note that completely removing retries might lead to messages not being delivered if there are temporary issues with the SMS gateway or network. It's generally recommended to have at least a few retries to improve the chances of successful delivery. Also, keep in mind that this configuration applies to both email and SMS messages sent by the Kratos courier. If you need different retry behavior for emails and SMS, you might need to consider a more complex setup or custom implementation.
n
@chilly-king-10285 from which version ory kratos support send verification via sms?
b
A message from kapa.ai
I apologize, but after carefully reviewing the provided knowledge sources, I don't have specific information about which version of Ory Kratos first introduced support for sending verification via SMS. The documents don't mention a particular version number for this feature. To get accurate information about the version history of Ory Kratos and when SMS verification was introduced, you might want to: 1. Check the official Ory Kratos changelog or release notes. 2. Look at the GitHub repository for Ory Kratos to see when the SMS verification feature was added. 3. Reach out to Ory support or community forums for more detailed version information. I apologize that I couldn't provide a more specific answer based on the given knowledge sources. If you need this information, it would be best to consult the official Ory documentation or contact Ory directly.
n
@chilly-king-10285 on ory kratos
v0.11.1
how to configure send verification code via sms
b
A message from kapa.ai
I apologize, but I don't have specific information about configuring SMS verification for Ory Kratos version 0.11.1 in the provided knowledge sources. The documentation I have access to doesn't mention version-specific configurations for this feature. However, I can provide you with general steps to configure SMS verification in Ory Kratos based on the most recent information available: 1. First, ensure your identity schema includes a phone number trait marked for verification via SMS:
Copy code
json {   "properties": {     "traits": {       "type": "object",       "properties": {         "phone": {           "type": "string",           "format": "tel",           "<http://ory.sh/kratos|ory.sh/kratos>": {             "verification": {               "via": "sms"             }           }         }       }     }   } }
[Source](https://www.ory.sh/docs/kratos/self-service/flows/verify-email-account-activation#phone-number-verification) 2. Configure the SMS courier channel in your Ory Kratos configuration:
Copy code
yaml courier:   channels:     - id: sms       type: http       request_config:         url: "<https://your-sms-gateway-url>"         method: POST         body: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgVG86IGN0eC5yZWNpcGllbnQsCiAgQm9keTogY3R4LmJvZHksCn0>=
[Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms) 3. Set up the SMS template for verification:
Copy code
yaml courier:   templates:     verification_code:       valid:         sms:           body:             plaintext: "<base64://WW91ciB2ZXJpZmljYXRpb24gY29kZSBpczoge3sgdHJpbSAuVmVyaWZpY2F0aW9uQ29kZSB9fQ>=="
[Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms#templates) 4. Enable the verification flow in your configuration:
Copy code
yaml selfservice:   flows:     verification:       use: code       enabled: true
[Source](https://www.ory.sh/docs/kratos/self-service/flows/verify-email-account-activation#configuration) Please note that these configurations are based on the most recent documentation available and may not be exactly applicable to version 0.11.1. I recommend checking the documentation specific to your version or upgrading to a more recent version of Ory Kratos for the most up-to-date features and configurations.
n
@chilly-king-10285 please give me example for this
Copy code
3. When triggering the verification flow, you can specify the method as "code" and provide only the phone number. This way, Kratos will send a verification code to the phone number without attempting to verify the email again. Here's an example of how you might structure the API request:
{
  "method": "code",
  "code": "PHONE_NUMBER_HERE"
}
How to trigger, where to, etc
b
A message from kapa.ai
I apologize for the confusion in the previous response. Let me provide you with a more accurate and detailed example of how to trigger the verification flow using the phone number. Based on the information from the knowledge sources, here's how you can do it: To trigger the verification flow for a phone number using the "code" method, you need to make a POST request to the verification endpoint. Here's a step-by-step guide: 1. First, initiate the verification flow by making a GET request to:
Copy code
GET https://$<http://PROJECT_SLUG.projects.oryapis.com/self-service/verification/api|PROJECT_SLUG.projects.oryapis.com/self-service/verification/api>
This will return a flow object with an ID. 2. Then, use the flow ID to submit the verification request:
Copy code
POST https://$<http://PROJECT_SLUG.projects.oryapis.com/self-service/verification?flow=<flow-id|PROJECT_SLUG.projects.oryapis.com/self-service/verification?flow=<flow-id>>
The request body should look like this:
Copy code
json    {      "method": "code",      "email": "+1234567890"    }
Note that we use the "email" field to provide the phone number, despite it being named "email". This is because the API uses the same field for both email and phone verification. [Source](https://www.ory.sh/docs/kratos/reference/api#tag/frontend/operation/updateVerificationFlow) Here's an example using cURL:
Copy code
sh # First, initiate the flow curl -X GET \   https://$<http://PROJECT_SLUG.projects.oryapis.com/self-service/verification/api|PROJECT_SLUG.projects.oryapis.com/self-service/verification/api>  # Then, submit the verification request curl -X POST \   -H "Content-Type: application/json" \   -d '{"method":"code","email":"+1234567890"}' \   https://$<http://PROJECT_SLUG.projects.oryapis.com/self-service/verification?flow=<flow-id|PROJECT_SLUG.projects.oryapis.com/self-service/verification?flow=<flow-id>>
Replace
$PROJECT_SLUG
with your actual project slug and
<flow-id>
with the ID you received from the first request. After submitting this request, Ory Kratos will send a verification code to the provided phone number. The user will then need to submit this code to complete the verification process. Remember to configure your SMS courier and templates correctly in your Ory Kratos configuration to ensure the verification code is sent properly. [Source](https://www.ory.sh/docs/kratos/emails-sms/sending-sms) This approach should trigger the verification flow and send a code to the specified phone number without attempting to verify the email again.