# Instagram Messaging

Instagram uses a numeric identifier for a sender and recipient. So-called **"Instagram Scoped User ID (IGSID)"** for the sender and an **"Instagram account ID"** for the recipient of the event. A person is assigned a unique IGSID for each Instagram Business account they start a conversation with.

These identifiers are visible in the CM.com platform as the `userid` below `channels...Instagram`.

### Supported Events

| Type            | Description                                                                                                                                      |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| MessageDelete   | Occurs when a message is deleted by the end-user.                                                                                                |
| StoryMention    | Occurs when a story mention is received.                                                                                                         |
| OptInSubscribed | When an user has subscribed on your opt-in messages.                                                                                             |
| OptInExtended   | When an user has extended their subscription on your opt-in messages by pressing the **Continue messages** button just before the token expires. |
| OptInStopped    | When a subscription on opt-in messages has been expired or an user has not extended their subscription on your opt-in messages.                  |
| OptInSuspended  | When an user suspends their opt-in messages subscription by pressing the **Stop these messages** button. Resulting in a temporarily block.       |
| OptInResumed    | When an user resumes their opt-in messages subscription after a suspension by pressing the **Resume these messages** button.                     |

### Custom Objects

**StoryMention** The object contains the story url

### MessageDelete

This event occurs when a message is deleted by the end-user on Instagram.

#### Example MessageDelete event

```json
{
    "from": {
        "number": "<IGSID>"
    },
    "to": {
        "number": "<IGID>"
    },
    "event": {
        "type": "MessageDelete",
        "reference": "453563216"
    },    
    "timeUtc": "2021-07-05T17:08:45",
    "channel": "Instagram"
}
```

## StoryMention

An Instagram business account can be notified when a user mentions them in a story.

> *Note* A Story mentioning webhook will only flow in if the user mentioning the business has their account setup as public. Story mentions from a private account will only flow in if the business follows the said account.

#### Example StoryMention event

```json
{
    "from": {
        "number": "<IGSID>"
    },
    "to": {
        "number": "<IGID>"
    },
    "event": {
        "type": "StoryMention",
        "reference": "453563217",
        "custom": {
               "url": "<CDN_URL>"
        }
    },
    "timeUtc": "2021-07-05T17:08:45",
    "channel": "Instagram"
}
```

## Marketing Messages events

There are various types of events that can occur when using Marketing Messages.

### Opt-In event

There are two scenarios for which you will receive this event:

1. When the user opts-in to receive your Marketing Messages based on a set frequency. (**OptInSubscribed**)
2. When the user clicks on the **Continue messages** button just before the token expires. This will refresh the NotificationToken of the user and extend the TokenExpiryTimestamp. (**OptInExtended**)

#### Example Opt-In Event (OptInSubscribed)

```json
{ 
  "from": { 
    "number": "Recipients IGSID" 
  }, 
  "to": { 
    "number": "Your IGID" 
  }, 
  "event": {
        "type": "OptInSubscribed",
        "reference": "A reference to the object or message that triggered the event",
        "custom": {
          "notificationToken": "123456789987654321",
          "tokenExpiry" : "2023-04-10T12:00:00Z"
        }
    },    
    "timeUtc": "2022-10-10T12:00:00",
    "channel": "Instagram"
}
```

### Re Opt-In event

Just before the token associated with the Marketing Message opt-in is about to expire, users will automatically receive a re opt-in request. Users will then have the choice of whether to continue receiving the Marketing Messages. The re opt-in request will only be sent to the user when the reoptin field of the original opt-in notification is set to True, otherwise the opt-in will stop automatically.

| Marketing Messages Frequency | Re Opt-In Request Sent |
| :--------------------------- | :--------------------- |
| Daily                        | After six months       |
| Weekly                       | After nine months      |
| Monthly                      | After twelve months    |

#### Example Re Opt-In Event (OptInExtended)

```json
{ 
  "from": { 
    "number": "Recipients IGSID" 
  }, 
  "to": { 
    "number": "Your IGID" 
  }, 
  "event": {
        "type": "OptInExtended",
        "reference": "A reference to the object or message that triggered the event",
        "custom": {
          "notificationToken": "123456789987654321",
          "tokenExpiry" : "2023-04-10T12:00:00Z"
        }
    },    
    "timeUtc": "2022-10-10T12:00:00",
    "channel": "Instagram"
}
```

### Manage Opt-In Event

Once users opt-in, they can easily manage their Marketing Messages, including stopping and resuming them, by clicking the “Manage” button. This will result in the following scenario’s:

1. When a user presses the **Stop these messages** button. Resulting in a temporary block of your messages. (**OptInSuspended**)
2. When a user presses the **Resume these messages** button after they have suspended your messages. (**OptInResumed**)

#### Example Manage Opt-In Event (OptInSuspended)

```json
{ 
  "from": { 
    "number": "Recipients IGSID" 
  }, 
  "to": { 
    "number": "Your IGID" 
  },
  "event": {
        "type": "OptInSuspended",
        "reference": "A reference to the object or message that triggered the event",
        "custom": {
          "notificationToken": "123456789987654321",
          "tokenExpiry" : "2023-04-10T12:00:00Z"
        }
    },    
    "timeUtc": "2022-10-10T12:00:00",
    "channel": "Facebook Messenger"
}
```

#### Example Manage Opt-In Event (OptInResumed)

```json
{ 
  "from": { 
    "number": "Recipients IGSID" 
  }, 
  "to": { 
    "number": "Your IGID" 
  },
  "event": {
        "type": "OptInResumed",
        "reference": "A reference to the object or message that triggered the event",
        "custom": {
          "notificationToken": "123456789987654321",
          "tokenExpiry" : "2023-04-10T12:00:00Z"
        }
    },    
    "timeUtc": "2022-10-10T12:00:00",
    "channel": "Instagram"
}
```