Why, why and how
It so happened that our project needed comments from VKontakte, but so that we could follow the comments. Since there are a lot of pages on the site, it is unrealistic to view all pages every day and check them
There were a lot of ways, for example, sending an e-mail message with each comment. If you use the "comment - alert" method, then there will be a lot of letters, but there is a way out - collecting new messages and sending one e-mail letter.
Watch and punish
Let's get started First of all, we climb into the
widget's documentation .
Add a widget to the page
<script src="http://userapi.com/js/api/openapi.js" type="text/javascript" charset="windows-1251"></script> <div id="vk_comments"></div> <script type="text/javascript"> VK.init({apiId: *******, onlyWidgets: true}); VK.Widgets.Comments("vk_comments", {limit: 10, width: "496", attach: "*", autoPublish: 0}); </script>
We read the documentation and see that the widget sends us two events via
VK.Observer with the parameters
num ,
last_comment ,
date ,
sign (for more information, see the link to the documentation):
1)
widgets.comments.new_comment - adding a new comment
2)
widgets.comments.delete_comment - delete comment
')
It turns out that we can follow the actions without problems. Since I need to get an answer and perform some actions, I will use jQuery. Add
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
Also before we add an action handler.
VK.Observer.subscribe('widgets.comments.new_comment', function(num,last_comment,date,sign){onComment(num,last_comment,date,sign,'new');}); VK.Observer.subscribe('widgets.comments.delete_comment',function(num,last_comment,date,sign){onComment(num,last_comment,date,sign,'del');});
+ notification function
function onComment(num,last_comment,date,sign,action){ $.ajax({ type: "POST", url: "test.php", cache: false, data:{"num":num,"last_comment":last_comment,"date":date,'sign':sign,'action':action}, success: function(html){
Now we set up our php script. Personally, I decided to use the method of recording new comments in the database with the value of the field
approve = 0 , in order to show the script the raw comments.
We will not write all the code, but we will write only the main thing - the authentication of the request and the type of action. To do this, we need to know the "Secure Application Key". Go to the "Administration" widget, "Assign Administrator", go to the menu item "Settings".
<? if(md5(' '.$_POST['date'].$_POST['num'].$_POST['last_comment'])==$_POST['sign']){ if($_POST['action']=='new'){
You can enter into the database the number of comments, the comments themselves, the commenting date.
Then, we write a script that will be called up using the crown, check the database for new comments and, if they exist, send a letter to the host.
You can also add an entry to the
$ _SERVER ['HTTP_REFERER'] parameter in the database to determine from which page the comment was sent.
When receiving a letter, we look at the presence of undesirable or interesting comments and punish / encourage users.
Disappointment
Unfortunately, I did not find in the documentation of events:
1) If the user deleted and restored the comment
2) Received a reply to the comment
Perhaps I was looking bad.
If something is not working - the
documentation of the widget