📜 ⬆️ ⬇️

Post news with a picture in the Vkontakte group (Perl)

When creating information resources, one often has to think about automating routine work. In this article we will consider a simple way, with the help of several Perl lines, as news, special offers, or other useful information, put on your page or group in social networks (Vkontakte). In this case, with a picture, text and a link.




Short plan:
1. Create an application vkontakte.
2. Genera Token to the application
3. Get the URL of the server for uploading images.
4. Upload the image to the previously received server.
5. Save the image on the server.
6. We post the news with a picture on the page.
')
1. create an application on the website of VKontakte.
At the bottom of the site there is a link for Developers, and the Create Application button.
Choose a name and standalone application.
After creating on the Settings tab of the application, you will see the ID of your application (remember).

2. Get the Token (the key with which the created script will be authorized on the site). To do this, open the page in the browser:
https://oauth.vk.com/authorize?client_id={ID}&scope=wall,photos,audio,video,docs,notes,groups,messages,notifications,stats,ads,notify,friends,offline&redirect_uri=http://oauth.vk.com/blank.html&display=page&response_type=token 

where
{ID} - id of the created application, for example 1234563,
scope is the rights for your application that it will be allowed.
(wall - post on the wall, photos - upload photos, groups - access to groups, messages - send messages, friends - access to friends and offline - so that the Token key never ends and the script can always access the site).
response_type = token - get Token.

Next, go to the script:

Input parameters:


 #!/usr/bin/env perl use strict; use LWP; use HTTP::Request::Common; my $token = '55e43443343435355a3e70e805722345552227'; #      my $gid = '65596688'; #  ID      . my $file = '/home/ds/test/2.jpg'; #     my $site_url = 'http://example.com'; #    ,        my $message = '  '; #     . 


3. Now we need to get the server VKontakte, where we will upload the image:


 #      getWallUploadServer my $url1 = "https://api.vkontakte.ru/method/photos.getWallUploadServer?gid=$gid&access_token=$token"; my $res_url = &get_data($url1); $res_url =~ s/([\\'])?//g; #   . $res_url = $1 if ($res_url =~ m/.*?upload_url\"\:\"(.*?)\".*?/); #  URL. my ($url , $param) = split(/\?/, $res_url); #  url       my %url_param = map {split("=")} split("&", $param); 


4. Upload the image to the specified url:


  my $ua = LWP::UserAgent->new (agent=>'Mozilla/5.0', requests_redirectable=>0); my $res = $ua->request ( POST "$url", Content_Type => 'multipart/form-data', Content => [ 'act' => $url_param{'act'}, 'mid' => $url_param{'mid'}, 'aid' => $url_param{'aid'}, 'gid' => $url_param{'gid'}, 'hash' => $url_param{'hash'}, 'rhash' => $url_param{'rhash'}, 'swfupload' => $url_param{'swfupload'}, 'api' => $url_param{'api'}, 'wallphoto' => $url_param{'wallphoto'}, 'photo' => [$file], ], ) die("$!"); my $res_upload = $res->as_string(); $res_upload =~ s/([\\'])?//g; #  . #      my $server_upload = $1 if ( $res_upload =~ m/server\"\:(.*?),\".*/); my $photo_upload = $1 if ( $res_upload =~ m/\"photo\"\:\"(.*?)\",\"hash.*/); my $hash_upload = $1 if ( $res_upload =~ m/\"hash\"\:\"(.*?)\"}$/); 


5. After that we need to record the downloaded image on the server:


  my $url2 = "https://api.vkontakte.ru/method/photos.saveWallPhoto?gid=$gid&access_token=$token&server=$server_upload&photo=$photo_upload&hash=$hash_upload"; my $res_save = get_data($url2); my $ph_id = $1 if ( $res_save =~ m/\"id\"\:\"(.*?)\",\".*/); #      


6. And the last thing to post it on the wall:


  my $url3 = "https://api.vkontakte.ru/method/"."wall.post?owner_id=-$gid&attachments=$ph_id,$site_url&from_group=1&access_token=$token"; #  url  . $url3 .="&message=$message" if ($message); #   ,   . my $res_post = get_data($url3); 

where owner_id should be with a "-" (minus), because this group.
from_group = 1 - post news on behalf of the group.

Finally, a small get_data () procedure that was accessed to transfer http data.
  my $url = shift; my $ua = LWP::UserAgent->new (agent=>'Mozilla/5.0', requests_redirectable=>0); my $get_url = $ua->request (GET "$url") die("$!"); my $res_url = $get_url->as_string(); return ($res_url); 


Several links to VKontakte for automation:
Sending a message:

 https://api.vkontakte.ru/method/messages.send?uid={UID}&message={MSG}&title={TITLE}&access_token={TOKEN} 

where UID is the ID of the user to whom the message is being sent (this is a numeric value, if a friend has an alphabetic id in the browser url, then you can go to his album and there will be numbers in the url), {MSG} is the message itself, {TITLE} - the message header, and your token.

Change your status. Here id is not needed, because The application is tied to your account.

 https://api.vkontakte.ru/method/status.set?text={TEXT}&access_token={TOKEN} 


Posting a simple message on the wall to the group.

 https://api.vkontakte.ru/method/wall.post?owner_id=-{GID}&message={MSG}&from_group=1&access_token={TOKEN} 

where {GID} is a digital group ID with a minus sign, {MSG} is a message, from_group - from whom there will be a message (1 - from a group (if you are an administrator)).

That's all. Of course, you need to also insert the code of checks and exit for errors, etc. But this is up to you.

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


All Articles