![](/img/trans.png)
[英]Whatsapp Api - How to automatically send pre-filled message which is in the url
[英]How to open a WhatsApp chat with a pre-filled message that spans on multiple lines from my website?
WhatsApp提供了一個自定義URL方案以與WhatsApp進行交互:
如果您有一個網站,並希望使用預填的消息來打開WhatsApp聊天,則可以使用我們的自定義URL方案來進行。 打開whatsapp:// send?text =,后跟要發送的文本,將打開WhatsApp,允許用戶選擇聯系人,並用指定的文本預填充輸入字段。
這是如何在您的網站上編寫此示例:
<a href="whatsapp://send?text=Hello%20World!">Hello, world!</a>
如果我想讓提到的pre-filled message
跨過這樣的多行怎么辦:
Text on the first line
Text on the second line
Text on the third line
Text on the fourth line
我嘗試將文本放在<a href=""></a>
標記中,如下所示:
<a href="whatsapp://send?text=First line\r\nSecond line\r\n\r\nThird line\r\nFourth line">Share on WhatsApp</a>
但這不起作用,該消息會在WhatsApp中顯示為:
First lineSecond lineThird lineFourth line
需要對<a href=""></a>
標記中的URL中使用的PHP換行符\\r\\n
進行編碼。 可以使用urlencode()
PHP函數執行此操作。 該鏈接必須類似於以下內容,以使用跨多行的預填充消息正確打開WhatsApp聊天:
<?php
$msg = "First line\r\nSecond line\r\n\r\nThird line\r\nFourth line";
$msg = str_replace("\r\n", urlencode("\r\n"), $msg); // note the double quotes
echo "<a href='whatsapp://send?text=$msg'>Share on WhatsApp</a>";
?>
現在,如果有人在他的Android或iOS設備上瀏覽您的網站時單擊該鏈接,那么WhatsApp應用程序將打開以允許他選擇聯系人,並在輸入字段中預填指定的文本,該文本將跨越多行,例如這個:
First line
Second line
Third line
Fourth line
注意必須使用雙引號,即"\\r\\n"
NOT '\\r\\n'
。
盡管urlencode($msg)
可在大多數移動瀏覽器上使用,但是它在android設備上的firefox上使用+
in編碼空格,因此您的用戶可能會看到諸如Text+on+the+first+line
類的Text+on+the+first+line
。 更好的解決方法是使用rawurlencode($msg)
使其與所有瀏覽器兼容,因為它會強制根據RFC 3986格式對文本進行編碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.