MMSを送信する
このコード・スニペットでは、Messages APIを使ってMMSを送信する方法を説明します。
重要: 現在、MMSの送信に対応しているのは、US Short Codes、10DLC番号、SMS Enabled Toll Free Numbersのみです。USショートコードの場合、MMSメッセージは米国内のAT&T、T-Mobile(旧Sprint)、Verizonネットワークに送信できます。 10DLC番号の設定について詳しくはこちら (注:このページはSMS APIを参照していますが、10 DLCガイドラインのセクションの内容はMessages APIにも適用されます)。
メッセージのスループット、配信可能性、SMSメッセージ量は、使用する番号の種類によって異なる場合があります。この点、およびMMS全般についての詳細は Vonage MMS概要ページその Vonage 10DLCの概要ページそして Vonage電話番号概要ページ.
例
各コード・スニペットで使用されているすべての変数の説明を以下に示します:
| キー | 説明 |
|---|---|
VONAGE_APPLICATION_ID | The Vonage Application ID. |
VONAGE_APPLICATION_PRIVATE_KEY_PATH | Private key path. |
VONAGE_PRIVATE_KEY_PATH | Private key path. |
FROM_NUMBER | The phone number you are sending the MMS from. (US Short Code, 10DLC number, or SMS Enabled Toll Free Number) |
VONAGE_NUMBER | Refer to |
VONAGE_FROM_NUMBER | Refer to |
TO_NUMBER | The number you are sending the to in E.164 format. For example |
IMAGE_URL | The URL of the media you want to send. Accepted file formats are |
注: 先頭の + または 00 電話番号を入力する場合は、14155550105のように国番号から入力してください。
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Write the code
Add the following to send-mms.sh:
curl -X POST "${MESSAGES_API_URL}" \
-H "Authorization: Bearer "$JWT\
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d $'{
"to": "'${MESSAGES_TO_NUMBER}'",
"from": "'${MMS_SENDER_ID}'",
"channel": "mms",
"message_type": "image",
"image": {
"url": "'${MESSAGES_IMAGE_URL}'"
}
}'
Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
npm install @vonage/server-sdk @vonage/messagesCreate a file named send-mms.js and add the following code:
const { Vonage } = require('@vonage/server-sdk');
const { Channels } = require('@vonage/messages');
/**
* It is best to send messages using JWT instead of basic auth. If you leave out
* apiKey and apiSecret, the messages SDK will send requests using JWT tokens
*
* @link https://developer.vonage.com/en/messages/technical-details#authentication
*/
const vonage = new Vonage(
{
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY,
},
{
...(MESSAGES_API_URL ? {apiHost: MESSAGES_API_URL} : {}),
},
);Write the code
Add the following to send-mms.js:
vonage.messages.send({
messageType: 'image',
channel: Channels.MMS,
image: {
url: MESSAGES_IMAGE_URL,
},
to: MESSAGES_TO_NUMBER,
from: MMS_SENDER_ID,
})
.then(({ messageUUID }) => console.log(messageUUID))
.catch((error) => console.error(error));Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Add the following to build.gradle:
implementation 'com.vonage:server-sdk-kotlin:2.1.1'Create a class named SendMmsImage and add the following code to the main method:
val client = Vonage {
applicationId(VONAGE_APPLICATION_ID)
privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
}Write the code
Add the following to the main method of the SendMmsImage class:
val messageId = client.messages.send(
mmsImage {
to(MESSAGES_TO_NUMBER)
from(MMS_SENDER_ID)
url(MESSAGES_IMAGE_URL)
caption(MESSAGES_CAPTION)
}
)Run your code
We can use the アプリケーション plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.kt.messages.mms with the package containing SendMmsImage:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'Create a class named SendMmsImage and add the following code to the main method:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();Write the code
Add the following to the main method of the SendMmsImage class:
var response = client.getMessagesClient().sendMessage(
MmsImageRequest.builder()
.from(MMS_SENDER_ID).to(MESSAGES_TO_NUMBER)
.url(MESSAGES_IMAGE_URL)
.build()
);
System.out.println("Message sent successfully. ID: "+response.getMessageUuid());Run your code
We can use the アプリケーション plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.messages.mms with the package containing SendMmsImage:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Install-Package VonageWrite the code
Add the following to SendMmsImage.cs:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var vonageClient = new VonageClient(credentials);
var request = new Vonage.Messages.Mms.MmsImageRequest
{
To = MESSAGES_TO_NUMBER,
From = MMS_SENDER_ID,
Image = new Attachment
{
Url = MESSAGES_IMAGE_URL
}
};
var response = await vonageClient.MessagesClient.SendAsync(request);Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
composer require vonage/clientCreate a file named send-mms.php and add the following code:
$keypair = new \Vonage\Client\Credentials\Keypair(
file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
VONAGE_APPLICATION_ID
);
$client = new \Vonage\Client($keypair);Write the code
Add the following to send-mms.php:
$image = new \Vonage\Messages\MessageObjects\ImageObject(
MESSAGES_IMAGE_URL,
'A MMS image message, with caption, sent using the Vonage Messages API'
);
$mms = new \Vonage\Messages\Channel\MMS\MMSImage(
TO_NUMBER,
FROM_NUMBER,
$image
);
$client->messages()->send($mms);Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
pip install vonage python-dotenvWrite the code
Add the following to send-image.py:
from vonage import Auth, Vonage
from vonage_messages import MmsImage, MmsResource
client = Vonage(
Auth(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_PRIVATE_KEY,
)
)
message = MmsImage(
to=MESSAGES_TO_NUMBER,
from_=MMS_SENDER_ID,
image=MmsResource(url=MESSAGES_IMAGE_URL),
)
response = client.messages.send(message)
print(response)Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
gem install vonageCreate a file named send-mms.rb and add the following code:
Run your code
Save this file to your machine and run it:
試してみる
コードを実行すると、宛先番号にMMSメッセージが送信されます。