pengenalan
Dalam tutorial ini, kami akan mencipta contoh mudah untuk menghantar mesej melalui API WhatsApp menggunakan Python.
Pada mulanya, anda mesti memastikan bahawa perpustakaan http.client sudah sedia Kerana kami akan menggunakan perpustakaan ini Dalam artikel ini untuk menghantar mesej ke Gerbang Ultramsg.
quicke Contoh penggunaan untuk WhatsApp API menggunakan Python
import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&body=WhatsApp API on UltraMsg.com works good&priority=10&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/chat", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Apabila kod sebelumnya dilaksanakan, dan jika mesej berjaya dihantar, respons akan menjadi seperti ini:
{"sent":"true","message":"ok","id":44897}
Hantar Imej
import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&image=https://file-example.s3-accelerate.amazonaws.com/images/test.jpg&caption=image Caption&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/image", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Hantar Dokumen
import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&filename=hello.pdf&document=https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/document", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Hantar Audio
import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&audio=https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/audio", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Hantar Suara
import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&audio=https://file-example.s3-accelerate.amazonaws.com/voice/oog_example.ogg&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/voice", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Hantar Video
import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&video=https://file-example.s3-accelerate.amazonaws.com/video/test.mp4&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/video", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Hantar Pautan
import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&link=https://en.wikipedia.org/wiki/COVID-19&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/link", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Hantar Kenalan
import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&[email protected]&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/contact", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Hantar Lokasi
import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&address=ABC company \n Sixth floor , office 38&lat=25.197197&lng=55.2721877&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/location", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Hantar Vcard
import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&vcard= BEGIN:VCARD\nVERSION:3.0\nN:lastname;firstname\nFN:firstname lastname\nTEL;TYPE=CELL;waid=14000000001:14000000002\nNICKNAME:nickname\nBDAY:01.01.1987\nX-GENDER:M\nNOTE:note\nADR;TYPE=home:;;;;;;\nADR;TYPE=work_:;;;;;;\nEND:VCARD&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/vcard", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
menggunakan permintaan lib
Kami juga boleh menggunakan Perpustakaan permintaan .
Contoh penggunaan untuk API WhatsApp menggunakan Python & requests lib
import requests
url = "https://api.ultramsg.com/instance16/messages/chat"
payload = "token=1v941eyo9eqixrsi&to=14155552671&body=test message;priority=1&referenceId="
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
akhirnya, anda boleh melihat Dokumentasi dan Soalan Lazim API Whatsapp Penuh .