constfrom=VONAGE_BRAND_NAMEconstto=TO_NUMBERconsttext='A text message sent using the Vonage SMS API'vonage.message.sendSms(from,to,text,(err,responseData)=>{if(err){console.log(err);}else{if(responseData.messages[0]['status']==="0"){console.log("Message sent successfully.");}else{console.log(`Message failed with error: ${responseData.messages[0]['error-text']}`);}}})
Add the following to the main method of the SendMessage class:
TextMessagemessage=newTextMessage(VONAGE_BRAND_NAME,TO_NUMBER,"A text message sent using the Vonage SMS API");SmsSubmissionResponseresponse=client.getSmsClient().submitMessage(message);if(response.getMessages().get(0).getStatus()==MessageStatus.OK){System.out.println("Message sent successfully.");}else{System.out.println("Message failed with error: "+response.getMessages().get(0).getErrorText());}
$response=$client->sms()->send(new\Vonage\SMS\Message\SMS(TO_NUMBER,BRAND_NAME,'A text message sent using the Nexmo SMS API'));$message=$response->current();if($message->getStatus()==0){echo"The message was sent successfully\n";}else{echo"The message failed with status: ".$message->getStatus()."\n";}
{"from":VONAGE_BRAND_NAME,"to":TO_NUMBER,"text":"A text message sent using the Nexmo SMS API",})ifresponseData["messages"][0]["status"]=="0":print("Message sent successfully.")else:print(f"Message failed with error: {responseData['messages'][0]['error-text']}")
vonage.messages.send(newWhatsAppText("This is a WhatsApp Message text message sent using the Messages API",TO_NUMBER,WHATSAPP_NUMBER),(err,data)=>{if(err){console.error(err);}else{console.log(data.message_uuid);}});
Add the following to the main method of the SendWhatsappText class:
varmessage=WhatsappTextRequest.builder().from(VONAGE_WHATSAPP_NUMBER).to(TO_NUMBER).text("Hello from Vonage!").build();try{MessageResponseresponse=messagesClient.sendMessage(message);System.out.println("Message sent successfully. ID: "+response.getMessageUuid());}catch(MessageResponseExceptionmrx){switch(mrx.getStatusCode()){default:throwmrx;case401:// Bad credentialsthrownewIllegalStateException(mrx.getTitle(),mrx);case422:// InvalidthrownewIllegalStateException(mrx.getDetail(),mrx);case402:// Low balanceclient.getAccountClient().topUp("transactionID");break;case429:// Rate limitThread.sleep(12_000);break;}}
Run the following gradle command to execute your application, replacing com.vonage.quickstart.messages.whatsapp with the package containing SendWhatsappText:
gradle run -Pmain=com.vonage.quickstart.messages.whatsapp.SendWhatsappText
Prerequisites
Install-Package Vonage
Write the code
Add the following to SendWhatsAppText.cs:
varcredentials=Credentials.FromApiKeyAndSecret(apiKey,apiSecret);varvonageClient=newVonageClient(credentials);varrequest=newWhatsAppTextRequest{To=to,From=brandName,Text="An SMS sent using the Vonage Messages API"};varresponse=awaitvonageClient.MessagesClient.SendAsync(request);
client=vonage.Client(application_id=VONAGE_APPLICATION_ID,private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,)client.messages.send_message({"channel":"whatsapp","message_type":"text","to":TO_NUMBER,"from":FROM_NUMBER,"text":"This is a WhatsApp text message sent using the Vonage Messages API",})
client=Vonage::Client.new(application_id: VONAGE_APPLICATION_ID,private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH))message=Vonage::Messaging::Message.whatsapp(type: 'text',message: "This is a WhatsApp Message text message sent using the Messages API")client.messaging.send(from: VONAGE_WHATSAPP_NUMBER,to: TO_NUMBER,**message)