📜 ⬆️ ⬇️

Bot for VK in Python with MySQL in an hour, part 1

On the Internet there are many tutorials how to write a bot for Vkontakte, but they all have significant drawbacks for beginners. They give a very minimal base because of which in the future it will be difficult for beginners to understand how certain things work.

In this tutorial, I want to show how to make the most simple bot with a fairly large basic functionality.

So, let's begin.
')

What do we need?



Connection


First, let's connect all the libraries:

import vk_api from vk_api.utils import get_random_id from from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType import pymysql.cursors import requests 

Now you can proceed to setting up the bot and database.

It is in my case that all the data is in the config.py file. You can register them anywhere. It will not affect the functionality in any way.

Database connection


To do this, create a function using the PyMySQL module:

 def getConnection(): connection = pymysql.connect(host='you_host', user='you_user', password='you_password', db='you_db' charset='utf8mb4', cursorclass=mymysql.cursors.DictCursor) return connection 

This function will return data for further work with the database. Accordingly, we substitute our data where we see "you_".

Setting up a connection to the bot


First we need to go to the settings of our group:



Select the Work with API tab and create an access key (in the future it will be useful to us to connect to the bot.



Go to the “Long Poll API” tab and enable it. And also choose the latest available version.



Go to the "Types of events" and also include everything that you need. I included everything for example



And the last. Go to "Messages"> "Settings for the bot" and enable the bots feature. Now you can go to the program itself.



Connect the bot to the group


Now let's set up the bot's connection to long poll and check actions by Vkontakte:

 vk_session = vk_api.VkApi(token=" ,      ") # vk_session = vk_api.VkApi(token = "a6f87v8c9a9sa87a7af9a0f9f9v8a6s6c5b5m6n8bds09asc8d7b87d87bd87n" vk = vk_session.get_api() longpoll = VkBotLongPoll(vk_session, "id   ") # longpoll = VkBotLongPoll(vk_session, "637182735") for event in longpoll.listen(): #  if event.type == VkBotEventType.MESSAGE_NEW: 

Now we have included the bot itself, which will receive incoming messages and analyze them for further work.

Work with bot


Let's add the bot function to respond to any of our messages with our own messages. To do this, after the last line, write this:

 if event.type == VkBotEventType.MESSAGE_NEW: #   #       if event.obj.text != '': #       if event.from_user: vk.messages.send( user_id=event.obj.from_id, random_id=get_random_id(), message=event.obj.text) 

Now about the vk.messages.send () method. This is the standard method of the vk_api module. You can read about the methods on the official documentation of Vkontakte (the link will be below).

It allows you to send messages to users.

The user_id argument indicates to which account this message should be sent. In our case, event.obj.from_id points to the account that just sent us a message. That is, we send a message to the person who sent it to us.
This argument is required!

The random_id argument is needed so that VKontakte does not send messages 2 times. This argument is required!

The message argument actually indicates which message we should send. The event.obj.text stores information about which text in the message we received. This argument is also required.

More about the arguments to this method can be found on the official page of Vkontakte.

Launch


Now our bot is ready and we can start it. After launch, you can safely write to the bot and see the result.

Total


On this part 1 is completed. We have learned to turn on the bot. Configure it, as well as send messages.

In Part 2, we will look at how to put messages into the database using the PyMySQL library and so on.

Thank you for reading!

Useful resources


Official documentation api Vkontakte
Official PyMySQL Module Page

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


All Articles