IMPORTANT, Please encodeURIComponent()
TL;DR You can use link below and place it on your button, or whatever. Its simply will open up whatsapp on user phone. But it isn't automatically send.
https://api.whatsapp.com/send?phone=+*YOURNUMBER*&text=%20*YOURMESSAGE*
What we were built here it is a dynamic send to WhatsApp link with JavaScript approach you can use the link above and give it a try
Okay I'll use JSBin with this, first open up then its just easy you will use split()
and join()
string property on JavaScript here my script.
// https://api.whatsapp.com/send?phone=+{{ *YOURNUMBER* }}&text=%20{{ *YOUR MESSAGE* }}
var yourNumber = "{{ your number in string}}"
var yourMessage = "{{ your message in string }}"
// %20 mean space in link
// If you already had an array then you just join them with '%20'
// easy right
function getLinkWhastapp(number, message) {
number = yourNumber
message = yourMessage.split(' ').join('%20')
return console.log('https://api.whatsapp.com/send?phone=' + number + '&text=%20' + message)
}
getLinkWhastapp()
Copy that on jsbin, give phone number and message do you want, make sure you use WhatsApp, It will open up WhatsApp on your phone with the message you wrote.
Further reading :
Important
This post was made to learn JS split
and join
for production purpose you should use encodeURIComponent()
function getLinkWhastapp(number, message) {
var url = 'https://api.whatsapp.com/send?phone='
+ number
+ '&text='
+ encodeURIComponent(message)
return url
}
Top comments (10)
Is there any way to send image with text?
No man its only accept url
Then maybe we can send url of Image?
Yes you can but it will convert to url text, not an image.
Ok, Thanks for reply
How can i use this for taking orders from our website ...like this👇
blantertokoshop.blogspot.com/2019/...
Yeah, maybe converting URLs to images should be a feature on Whatsapp.
Maybe media attribute
link
might work for this? There ispreview url
toowhatsapp cloud api docs: messages - media object
maybe use encodeURIComponent() to encode this.message to URI
Yeah thats nice, easy, error prone