{"id":1320,"date":"2022-03-02T15:09:30","date_gmt":"2022-03-02T15:09:30","guid":{"rendered":"https:\/\/blog.ultramsg.com\/receber-whatsapp-messages-using-php-and-webhook\/"},"modified":"2022-03-12T05:18:01","modified_gmt":"2022-03-12T05:18:01","slug":"receber-whatsapp-messages-using-php-and-webhook","status":"publish","type":"post","link":"https:\/\/blog.ultramsg.com\/pt-br\/receber-whatsapp-messages-using-php-and-webhook\/","title":{"rendered":"Receba mensagens do WhatsApp usando PHP e webhook"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"introduction\">Introdu\u00e7\u00e3o<\/h2>\n\n<p>Neste tutorial, vamos criar uma p\u00e1gina para lidar com mensagens recebidas do WhatsApp usando PHP.<\/p>\n\n<h2 class=\"wp-block-heading\" id=\"dependencies\">Depend\u00eancias<\/h2>\n\n<p>para fins de desenvolvimento local, \u00e9 necess\u00e1rio um servi\u00e7o de tunelamento. Este exemplo usa <a href=\"https:\/\/ngrok.com\/\" rel=\"nofollow noopener\" target=\"_blank\">o ngrok<\/a> , voc\u00ea pode baixar o ngrok <a href=\"https:\/\/ngrok.com\/download\" data-type=\"URL\" data-id=\"https:\/\/ngrok.com\/download\" rel=\"nofollow noopener\" target=\"_blank\">aqui<\/a> .<\/p>\n\n<h2 class=\"wp-block-heading\" id=\"example\">Exemplo<\/h2>\n\n<p>Este \u00e9 um exemplo muito simples, com o servidor registrando o corpo da solicita\u00e7\u00e3o no arquivo log.txt.<\/p>\n\n<p>O corpo cont\u00e9m as informa\u00e7\u00f5es completas do webhook enviadas da inst\u00e2ncia do WhatsApp da API Ultramsg.<\/p>\n\n<pre class=\"wp-block-code\"><code><code data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">$data = file_get_contents(\"php:\/\/input\");\n$event = json_decode($data, true);\nif(isset($event)){\n\t\/\/Here, you now have event and can process them how you like e.g Add to the database or generate a response\n\t$file = 'log.txt';  \n\t$data =json_encode($event).\"\\n\";  \n\tfile_put_contents($file, $data, FILE_APPEND | LOCK_EX);\n<\/code>}<\/code><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"on-server-usage\">No uso do servidor<\/h2>\n\n<p>Salve o arquivo de exemplo acima como test.php e fa\u00e7a o upload para o seu servidor, a URL do Webhook ser\u00e1:<\/p>\n\n<pre class=\"wp-block-code\"><code><a href=\"http:\/\/your-server\/webhook.php\"><code data-enlighter-language=\"powershell\" class=\"EnlighterJSRAW\">http:\/\/your-server\/test.php<\/code><\/a><\/code><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"on-local-usage-with-ngrok\">No uso local com ngrok<\/h2>\n\n<p>Salve o arquivo de exemplo acima como test.php em seu localhost e a URL ser\u00e1:<\/p>\n\n<pre class=\"wp-block-code\"><code><code data-enlighter-language=\"powershell\" class=\"EnlighterJSRAW\">http:\/\/localhost\/test.php<\/code><\/code><\/pre>\n\n<p>Iniciar ngrok:<\/p>\n\n<pre class=\"wp-block-code\"><code><code data-enlighter-language=\"powershell\" class=\"EnlighterJSRAW\">ngrok http 80<\/code><\/code><\/pre>\n\n<p>Depois disso, voc\u00ea deve ver um<br\/>*.ngrok.io URL.<br\/>por exemplo :<br\/>https:\/\/7647-115-83-121-164.ngrok.io<br\/>substitua localhost por<br\/>URL do ngrok.io<br\/>a URL ser\u00e1:<br\/>https:\/\/7647-115-83-121-164.ngrok.io\/webhook.php<\/p>\n\n<h2 class=\"wp-block-heading\" id=\"what-s-next\">Qual \u00e9 o pr\u00f3ximo?<\/h2>\n\n<p>cole seu URL nas configura\u00e7\u00f5es da inst\u00e2ncia.<br\/>Voc\u00ea deve poder receber webhooks agora e pode v\u00ea-los no arquivo log.txt.<\/p>\n\n<h2 class=\"wp-block-heading\" id=\"example-json-response\">EXEMPLO DE RESPOSTA JSON <\/h2>\n\n<pre class=\"wp-block-code\"><code>{\n  \"event_type\": \"message_received\",\n  \"instanceId\": \"90\",\n  \"data\": {\n    \"id\": \"false_10172127174@c.us_7ECAED9EB68D3474BE591443134C2E3F\",\n    \"from\": \"10172127174@c.us\",\n    \"to\": \"10172127175@c.us\",\n    \"ack\": \"pending\",\n    \"type\": \"chat\",\n    \"body\": \"I can't send a message using php code\\nCan you help me\",\n    \"fromMe\": false,\n    \"isForwarded\": false,\n    \"time\": 1643311467\n  }\n}<\/code><\/pre>\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Receive WhatsApp messages using PHP | WhatsApp api Webhook\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/TfQOclLMIng?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n<h2 class=\"wp-block-heading\" id=\"useful-links\">Links \u00dateis<\/h2>\n\n<ul class=\"wp-block-list\"><li>Documenta\u00e7\u00e3o <span style=\"font-family: -apple-system, system-ui, BlinkMacSystemFont, \" segoe=\"\" ui=\"\" helvetica=\"\" arial=\"\" sans-serif=\"\" color=\"\" emoji=\"\" symbol=\"\">completa da<\/span> <a style=\"font-family: -apple-system, system-ui, BlinkMacSystemFont, \" segoe=\"\" ui=\"\" helvetica=\"\" arial=\"\" sans-serif=\"\" color=\"\" emoji=\"\" symbol=\"\" href=\"https:\/\/docs.ultramsg.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">API do Whatsapp<\/a> <span style=\"font-family: -apple-system, system-ui, BlinkMacSystemFont, \" segoe=\"\" ui=\"\" helvetica=\"\" arial=\"\" sans-serif=\"\" color=\"\" emoji=\"\" symbol=\"\">.<\/span><\/li><li>enviar mensagens <a href=\"https:\/\/blog.ultramsg.com\/pt-br\/enviar-whatsapp-message-by-whatsapp-api-using-php\/\">da API do WhatsApp<\/a> usando PHP.<\/li><\/ul>\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introdu\u00e7\u00e3o Neste tutorial, vamos criar uma p\u00e1gina para lidar com mensagens recebidas do WhatsApp usando &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Receba mensagens do WhatsApp usando PHP e webhook\" class=\"read-more button\" href=\"https:\/\/blog.ultramsg.com\/pt-br\/receber-whatsapp-messages-using-php-and-webhook\/#more-1320\" aria-label=\"Read more about Receba mensagens do WhatsApp usando PHP e webhook\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":1212,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[115,117,125],"class_list":["post-1320","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-api-do-whatsapp","tag-php-pt-br","tag-tutoriais","tag-webhook-pt-br","infinite-scroll-item","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-25","no-featured-image-padding"],"_links":{"self":[{"href":"https:\/\/blog.ultramsg.com\/pt-br\/wp-json\/wp\/v2\/posts\/1320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ultramsg.com\/pt-br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ultramsg.com\/pt-br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ultramsg.com\/pt-br\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ultramsg.com\/pt-br\/wp-json\/wp\/v2\/comments?post=1320"}],"version-history":[{"count":4,"href":"https:\/\/blog.ultramsg.com\/pt-br\/wp-json\/wp\/v2\/posts\/1320\/revisions"}],"predecessor-version":[{"id":1801,"href":"https:\/\/blog.ultramsg.com\/pt-br\/wp-json\/wp\/v2\/posts\/1320\/revisions\/1801"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ultramsg.com\/pt-br\/wp-json\/wp\/v2\/media\/1212"}],"wp:attachment":[{"href":"https:\/\/blog.ultramsg.com\/pt-br\/wp-json\/wp\/v2\/media?parent=1320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ultramsg.com\/pt-br\/wp-json\/wp\/v2\/categories?post=1320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ultramsg.com\/pt-br\/wp-json\/wp\/v2\/tags?post=1320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}