# MessageML

You can use MessageML to implement conversational messaging with
[incoming message webhooks](webhooks.md). MessageML is an XML-based set of instructions that you can use to respond to incoming SMS.

## Example

MessageML is an XML document with a root `<Response>` element
containing child **action** elements. For example:

```xml
<Response>
  <Message>Thanks for texting!</Message>
  <Redirect>do-next-action.php</Redirect>
</Response>
```

Return the MessageML with a `Content-Type` of `text/xml` or `application/xml`. Enfonica
ignores a response with another content type.

## Actions

Actions are executed sequentially through the MessageML response. There are
four actions available in MessageML.

### Action: Message

The message action can be used to send a message. By default, it replies to the sender of the message
you have received. A message that this action sends is checked against your project's unsubscribers, so it
isn't sent to someone who has replied STOP. You can configure the behavior of the message action with the
following attributes.

Attribute | Description | Default
--|--|--
To | The recipient of the message in E164 format. | The sender of the incoming message being processed.
From | The sender ID to use when responding. | The phone number the message was received on.
ReplyUri | The URI (absolute or relative) to send any replies to. | The current URL.
StatusUpdateUri | The URI (absolute or relative) to send [Status Update Webhooks](webhooks.md) to. | -

The content of the Message element is used as the body of the message.

A maximum of 10 message actions can be used in a MessageML response.

#### Example: reply to an incoming message

```xml
<Response>
  <Message>Thanks for texting!</Message>
</Response>
```

### Action: Unsubscribe

The unsubscribe action is used to unsubscribe the sender from the project.

#### Example: unsubscribe the sender

```xml
<Response>
  <Unsubscribe/>
</Response>
```

### Action: Redirect

The redirect action can be used to transfer control of handling an incoming
message to another URI. The contents of the Redirect element is used as the
location to redirect to, and can either be an absolute or relative URI.

No further actions are executed after a redirect action.

A single incoming message can be redirected a maximum of 10 times.

#### Example: redirect to an alternative handler

```xml
<Response>
  <Redirect>/alternative-handler.php</Redirect>
</Response>
```

### Action: Label

The `<Label>` action allows you to set metadata on the message. This can be useful for persisting data for reporting or diagnosis purposes.

You can access labels using the Enfonica Console or the Messaging API.

An incoming message starts with the labels of the number that it was sent to, and they count towards the limit of 50 labels on a message.

Attribute | Allowed Values | Default
--|--|--
Key (required) | string | none

The body of the element represents the value to set against the label. An empty value will remove the key from the message's labels.

#### Example 1: Set a label

The following sets the label `customer_id` to value `540`.

```xml
<Response>
    <Label Key="customer_id">540</Label>
</Response>
```

#### Example 2: Remove a label

The following removes the label `customer_id`.

```xml
<Response>
    <Label Key="customer_id"></Label>
</Response>
```

## Cookies

Incoming message webhooks maintain cookies for up to 30 days between two
parties in an SMS conversation. You can use cookies to easily maintain state
in a conversation by either setting cookies directly, or using the in-built
session state in your programming language or framework of choice.
