📜 ⬆️ ⬇️

We write the Slack bot to get comments from VK in Python

Expanding the basic features of Slack, I encountered the problem of integrating some social networks into chat rooms.

To make friends with Slack and Vkontakte, it was decided to use Python and API. Below is a basic recipe on how to teach a Slackbot to get information about the latest comments from posts on the wall of Vkontakte groups or communities.

Slack api


The popular Slack Messenger has a convenient and simple API . With it, you can easily manage chat bots and carry out various manipulations with channels. Here is the easiest way to create a chat bot:

1. Open Slack.com (you must already be registered in it to manage your team).
2. Go to slack.com/apps application management and open Configure .
3. Open “Custom Integrations” and create your first bot for Slack. (Come up with a name, select the room where he will send messages)
4. Slack will give you the API token, which you will use to authorize the bot.
')

Slack-Bot.py


The initial stage is completed, let's start writing the bot. To work, we need an installed Python 3 and some additional libraries for working with the API. Install slacker and VK_api :

$ pip install slacker $ pip install vk_api 

We create the base Slack-Bot.py file in any code editor and start writing our bot.

Import the necessary libraries:

 from slacker import Slacker import vk_api 

Log in token to Slack:

 slack = Slacker('  API token') 

Log in to VK.com:

 login, password = 'Login', 'pass' vk_session = vk_api.VkApi(login, password) try: vk_session.authorization() except vk_api.AuthorizationError as error_msg: print(error_msg) return vk = vk_session.get_api() 

And now we will teach our bot to get information from the wall of Vkontakte groups:

 man_id = str(-29534144) #id ,        postidlist = vk.wall.get(owner_id=man_id, count=1, offset=0) #     a = str(postidlist['items'][0]['id']) # id       response = vk.wall.getComments(owner_id=man_id, post_id=a, count=1, sort='desc', offset=0) #        

According to the standard, the answer comes in the form of an array. To pull out the text, you need to select the lines of interest:

 b = response['items'][0]['text'] #     

Now the received text is sent by the bot to Slack (the #vk parameter means the room where the bot will send the message):

 slack.chat.post_message('#vk', ' : ' + b) #     Slack 

So we got the basic functionality for receiving comments from the wall of Vkontakte groups and sending them to the Slack rooms.

The logic of the script is as follows:

1. Log in to Slack and Vkontakte;
2. We request the last post from the selected group;
3. We get the last comment from the post;
4. We send the comment to Slack.

You can modify the bot and expand its functionality by adding a check to the group wall at regular intervals and send new comments to Slack.

The final code of the working bot:

 from slacker import Slacker import vk_api import time slack = Slacker(' token Slack bot') def main(): login, password = 'login', 'pass' vk_session = vk_api.VkApi(login, password) try: vk_session.authorization() except vk_api.AuthorizationError as error_msg: print(error_msg) return vk = vk_session.get_api() while True: # man_id = str(-29534144) #id         postidlist = vk.wall.get(owner_id=man_id, count=1, offset=0) #   a = str(postidlist['items'][0]['id']) # id       time.sleep(5) ts = 10 bts = 10 while ts == bts: #    ,     ,   . response = vk.wall.getComments(owner_id=man_id, post_id=a, count=1, sort='desc', offset=0) #   ts = str(response['items'][0]['date']) #   time.sleep(5) # 5  response = vk.wall.getComments(owner_id=man_id, post_id=a, count=1, sort='desc', offset=0) bts = str(response['items'][0]['date']) print('   ' + ts + bts) time.sleep(5) #       response = vk.wall.getComments(owner_id=man_id, post_id=a, count=1, sort='desc', offset=0) #     b = response['items'][0]['text'] #     slack.chat.post_message('#vk', ' : ' + b) #     Slack    if __name__ == '__main__': main() 

The full code can be found here . Thanks for attention.

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


All Articles