Messages from the site in VK - simply and efficiently - PHP + CUrl

Delivery of messages to the VC is convenient for most sites of different directions: many do not read emails, everything is difficult in such messengers like Votsap and Weiber. And sending news and personal notifications from the site to the VC is the most - easy, simple and immediately visible to the user.

For my notification service for new ads, I need to deliver notifications about new ads to users quickly and free of charge. The base channel - email - not convenient. Another good channel - Telegram messenger - decided to fight for privacy and became unreliable. Therefore, we had to add an alternative in the form of VC (especially since the service can still track groups in the VC for new entries and comments). I was pleasantly surprised by the simplicity and thoughtfulness of the setting, even the Telegram did not stand nearby.

1. Connecting the widget "Allow to write to the community"

The widget looks like a button. I have embedded a button in the service user account settings.

image

In the text, you need to replace 74449217 with the id of your group. Also consider where the userId VKontakte will be saved. In the code below, it is assigned to the vk_id field, and then saved along with other settings.

<script type='text/javascript' src='https://vk.com/js/api/openapi.js?154'></script> <div id='vk_send_message'></div> <script type='text/javascript'> VK.Widgets.AllowMessagesFromCommunity('vk_send_message', {height: 30},74449217); VK.Observer.subscribe('widgets.allowMessagesFromCommunity.allowed', function f(userId) { console.log(userId); console.log('allowed'); document.getElementById('vk_id').value=userId; }); VK.Observer.subscribe('widgets.allowMessagesFromCommunity.denied', function f(userId) { console.log(userId); console.log('denied'); document.getElementById('vk_id').value=''; }); </script> 

Widget documentation .

We received permission from the user; now we will organize the sending of messages.

2. Sending messages

In the settings of your community include community posts.

image

We generate a token of our community.

image

In general, everything is ready. To send a message, use PHP + CUrl. In the function of sending as input parameters we transfer the previously received user ID VK and the text of the message. The function also uses vk_token constants - the community token.

 //   vk function send_vk($vk_id, $text_source) { $url="https://api.vk.com/method/messages.send?user_id=$vk_id&v=5.76&access_token=".vk_token; $text=rawurlencode($text_source); $ch = curl_init($url); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_POSTFIELDS, "&message=".$text); $hh=curl_exec($ch); //    if (!strpos(" ".$hh,'{"response":')) { //    ,   if (!strpos(" ".$hh,'error')) {$hh=curl_exec($ch);} } $html=$hh; curl_close($ch); return $html; } 

Along with studying the possibilities and material, I spent on organizing sending alerts to VC one day - I think this is one of the easiest ways to communicate with the user.

Source: https://habr.com/ru/post/416001/


All Articles