POST graph.chat.messages

Create new messages, modify chats, dialogs and their participants

This method allows you to create new chat messages

Request example

https://api.ok.ru/graph/chat:C3ecb9d02a600/messages
   ?access_token=tkn18YdUJZe:CQABPOJKAKEKEKEKE

Sending messages

This is a key feature of this method.

To create a new message you need to send a POST-payload data with a message object which is similar to an object that you can get with me/messages/get method.

Text message example:

{
  "recipient":{"chat_id":"chat:C3ecb9d02a600"},         /* Chat ID in a chat:id format */
  "message":{                                           /* Message content */
    "text":"Привет"                                     /* Message text */
  }
}

Sending messages with attachments

A message with image and file attachments can be sent.

A message with an IMAGE attachment can be sent like this:

{
  "recipient":{"chat_id":"chat:C3ecb9d02a600"},                     /* Chat ID in a chat:id format */
  "message":{                                                       /* Message content */
    "attachment":{
      "type":"IMAGE",
      "payload":{"url":"https://st.mycdn.me/res/i/ok_logo.png"}     /* Resource URL */
    }
  }
}

A message with a FILE attachment can be sent like this:

{
  "recipient":{"chat_id":"chat:C3ecb9d02a600"},                     /* Chat ID in a chat:id format */
  "message":{                                                       /* Message content */
    "attachment":{
      "type":"FILE",
      "payload":{"ref":"fileId"}                                    /* Temporary file identifier */
    }
  }
}

More info about file uploading can be found here - graph.user.fileUploadUrl

Direct user messages

It is possible to send a message from group to a single or multiple users at once.

User must allow to receive group messages before it can be sent to him.

Following message syntax is used for this purpose:

{
    "recipient":{
        "user_ids":[                                /* List of user ids in user:id or id format*/
            "user:123456789012",                    
            "210987654321"
        ]
    },
    "message":{                                     /* Message content */
        "text":"Hello"                              /* Message text */
    }
}

Each user from the list will get a message in his own group-to-user chat.

API will respond with two corresponding list of values: * boolean type values list that indicate if message was or was not successfully sent; * list of user-to-group chat ids in which messages where sent.

{
  "success": [
    true,
    false
  ],
  "chat_ids": [
    "chat:C401b13206b00",
    null
  ]
}

To send a message directly to one user you can use a simplified message syntax:

{
    "recipient":{
        "user_id": "user:123456789012"              
    },
    "message":{                                     
        "text":"Hello"                              
    }
}

Message format

{
  "message": {
    "text": "String", /* Message text */
    "attachment": { 
      "type": "IMAGE|VIDEO|AUDIO|SHARE|FILE|CONTACT|INLINE_KEYBOARD|LOCATION|MUSIC|CALL|PRESENT|STICKER", /* attachment type */
      "payload": AttachmentPayload /* attachment body, depends on type */
    }, /* single message attachment */
    "attachments": [{
      "type": "IMAGE|VIDEO|AUDIO|SHARE|FILE|CONTACT|INLINE_KEYBOARD|LOCATION|MUSIC|CALL|PRESENT|STICKER",
      "payload": AttachmentPayload
    }], /* multiple message attachments */
    "privacyWarning": "SCREENSHOT|SCREENCAST",
    "reply_to": "MID:" /* responded message id */
  }
}

Attachments

Message can contain a single or multiple attachments of following type:

  • IMAGE - image;
  • VIDEO - video;
  • AUDIO - audio;
  • SHARE - OK content reshare;
  • FILE - file of any extension;
  • CONTACT - user contact;
  • INLINE_KEYBOARD - action buttons structure;
  • LOCATION - user location;
  • MUSIC - OK music track;
  • CALL - information about video / audio call;
  • PRESENT - OK present;
  • STICKER - OK sticker.

Attachment’s payload depends on its type and can differ.

IMAGE

The image attachment.

    
{
  "id": "imageId",
  "token": "imageToken",
  "url": "https://image.url"
}

There are following ways to send such attachment to a chat:

  • specify token from existing message with attachment
  • specify token, acquired during image upload using method GET graph.user.fileUploadUrl method
{
  "token": "imageToken"
}
  • specify image URL, but only 1 such image can be present in request
{
  "url": "https://image.url"
}

VIDEO

The video attachment.

{
  "id": "videoId",
  "token": "videoToken",
  "url": "https://video.url"
}

There are following ways to send such attachment to a chat:

  • specify token from existing message with attachment
  • specify token, acquired during video upload using method GET graph.user.fileUploadUrl method
{
  "token": "videoToken"
}

AUDIO

The audio attachment.

{
  "id": "audioId",
  "token": "audioToken",
  "url": "https://audio.url"
}

There are following ways to send such attachment to a chat:

  • specify token from existing message with attachment
  • specify token, acquired during audio upload using method GET graph.user.fileUploadUrl method
{
  "token": "audioToken"
}

FILE

File of any format.

{
  "id": "fileId",
  "token": "fileToken",
  "url": "https://file.url"
}

There are following ways to send such attachment to a chat:

  • specify token from existing message with attachment
  • specify token, acquired during audio upload using method GET graph.user.fileUploadUrl method
{
  "token": "fileToken"
}

SHARE

Reshare of a content previously published on OK. This attachment can contain a link to a group, video, group / user topic, image, etc.

{
  "id": "123456789",                                        
  "url": "https://ok.ru/group123456789/topic/123456789"
}

Creation of this type of attachment via botapi is not supported.

CONTACT

OK’s user contact.

{
  "id": "123456789",                /* user identifier */
  "name": "firstName lastName",     /* username */
  "photoUrl": "https://photo.url",  /* link to user's avatar */
  "phone": "79493344555",           /* user phone number */
  "vcfBody": "..."                  /* vCard electronic business card */
}

There are following ways to send such attachment to a chat:

  • specify user identifier
{
  "id": "123456789"
}
  • specify vCard electronic business card data
{
  "vcfBody": "..."
}

LOCATION

User’s location.

{
  "latitude": 59.928658,
  "longitude": 30.38113,
  "altitude": 1.0000,
  "epu": 1.0000,
  "heading": 1.0000,
  "speed": 1.0000,
  "zoom": 1.0000,
  "livePeriod": 600  /* time during which the user will share the live location, in seconds */
}

There are following ways to send such attachment to a chat:

  • specify coordinates latitude and longitude (other fields are optional)
{
  "latitude": 59.928658,
  "longitude": 30.38113
}

MUSIC

OK music track.

{
  "id": "23486020457601",                           
  "url": "https://ok.ru/music/track/23486020457601" 
}

There are following ways to send such attachment to a chat:

  • specify track identifier
{
  "id": "23486020457601",                           
}

CALL

Video/audio call info.

{
  "id": "23486020457601",                           /* call identifier */
  "type": "AUDIO|VIDEO",                            /* call type */
  "hangupType": "CANCELED|REJECTED|HUNGUP|MISSED"   /* call hangup type */
  "duration": 10                                    /* call duration */
}

Creation of this type of attachment via botapi is not supported.

PRESENT

This type of attachment is currently not supported

OK present.

{
  "id": "23486020457601",            /* present identifier */
  "status": "SENT",                  /* present status */
  "receiverId": "USER:12345678901",  /* receiver id */
  "senderId": "USER:12345678902"     /* sender id */
}

Creation of this type of attachment via botapi is not supported.

STICKER

OK sticker.

{
  "id": "c23a918ef4",                                         
  "url": "https://i.mycdn.me/getSmile?smileId=c23a918ef4"     
}

There are following ways to send such attachment to a chat:

  • specify sticker identifier
{
  "id": "c23a918ef4"                                         
}

INLINE_KEYBOARD

REQUEST_GEO_LOCATION and REQUEST_CONTACT type buttons are currently not supported

Buttons structure.

{
    "keyboard": {
        "buttons": [
            [
                {
                    "type": "CALLBACK",                     /* button type */
                    "text": "someText",                     /* button text */
                    "intent": "DEFAULT|POSITIVE|NEGATIVE",  /* button color scheme */
                    "payload": "callbackPayload"            /* payload text */
                }
            ],
            [
                {
                    "type": "LINK",
                    "text": "someText",
                    "intent": "DEFAULT|POSITIVE|NEGATIVE",
                    "url": "https://some.url"               
                }
            ],
            [
                {
                    "type": "REQUEST_CONTACT",
                    "text": "someText",
                    "intent": "DEFAULT|POSITIVE|NEGATIVE"
                },
                {
                    "type": "REQUEST_GEO_LOCATION",
                    "text": "someText",
                    "intent": "DEFAULT|POSITIVE|NEGATIVE",
                    "quick": true
                }
            ]
        ]

    },
    "callbackId": "16ef50d9a4e00c516ef50d9a4e00c516ef50d9a4e00c5"   /* callback identifier */
}

Buttons list is a two dimensional array where buttons can be placed one on a single line or as a group.

There are following buttons types supported:

  • CALLBACK - main type of a button. When pressed by a user a new message is sent to a chat (called callback message);
  • LINK - a button that contains a link to another URL;
  • REQUEST_CONTACT - request of current user’s contact info;
  • REQUEST_GEO_LOCATION - request of current user’s location info.