E
E
Electr1k2020-11-21 16:23:29
Python
Electr1k, 2020-11-21 16:23:29

Need code to sort VK id?

I wrote a bot for VK in python.
Here is the code:

from tkinter import *
import pyautogui as pg
import pyautogui
import time
import vk_api
from vk_api.longpoll import  VkLongPoll, VkEventType
window = Tk()
window.iconbitmap("zbot.ico")
token = *скрыт*
vk_session = vk_api.VkApi(token=token)
vk = vk_session.get_api()
longpoll = VkLongPoll(vk_session)
upload = vk_api.VkUpload(vk)
user_id_k = *скрыто*
while True:
   for event in longpoll.listen():
       if event.type == VkEventType.MESSAGE_NEW:
          if event.to_me:
              msg = event.text.lower()
              if msg == '!старт':
                 pg.click(220, 1030)
                 time.sleep(0.1)
                 pg.doubleClick(216, 538)
                 time.sleep(0.1)
                 pg.click(1564, 9)
                 vk.messages.send(user_id=user_id_k, message='Запущена!', random_id=0)
              elif msg == '!стоп':
                  pg.click(button='right', x=74, y=1032)
                  time.sleep(0.7)
                  pg.click(75, 989)
                  vk.messages.send(user_id=user_id_k, message='Остановлена!', random_id=0)
              elif msg == '!скрин':
                  screen = pyautogui.screenshot('im.vk.png')
                  photo = upload.photo_messages('im.vk.png')
                  owner_id = photo[0]['owner_id']
                  photo_id = photo[0]['id']
                  access_key = photo[0]['access_key']
                  attachment = f'photo{owner_id}_{photo_id}_{access_key}'
                  vk.messages.send(peer_id=user_id_k, random_id=0, attachment=attachment)
              else:
                  vk.messages.send(user_id=user_id_k, message='Не знаю такого!', random_id=0)

The bot, when receiving a certain message, clicks on a certain point on the screen.
The bottom line is that if someone writes one of the commands to the bot, the bot will execute it and send a report in the message (written in the code). So, how to make the bot respond only to my messages, that is, if several people (or one) write to him, he should find only me among them and read only the dialogue with me (execute commands from the dialogue). On reflection, I came to the conclusion that the bot should compare the id of the user who wrote the message and my id, if the ids converge to execute commands, otherwise ignore it. Is there any library for this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WolfInChains, 2020-11-21
@Electr1k

You were also answered in your previous question - to compare your ID with the sender's ID. For this, some other library is not needed, you are already using vk_api, which returns the sender's ID.

if event.user_id == ваш айди:
    #выполняем код

Or
if event.from_me:
    #выполняем код

But the second option is only suitable if you send a command from the bot page

M
Mors Clamor, 2020-11-22
@66demon666

Oh those programmers!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question