F
F
fodiche2021-08-04 17:25:02
Python
fodiche, 2021-08-04 17:25:02

Why does the command in the vk_api bot not work?

Everything seems to start, but when I write 'hello' the bot is just silent

import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
from vk_api.bot_longpoll import VkBotEventType, VkBotLongPoll
import random
import time
import datetime
import sqlite3 

db = sqlite3.connect('server.db')
cursor = db.cursor()

db.execute("""CREATE TABLE IF NOT EXISTS users (
  money BIGINT,
  biz TEXT,
  profitbiz BIGINT,
  user_id INT
)""")

db.commit()

vk_session = vk_api.VkApi(token="")
longpoll = VkLongPoll(vk_session)

print("YES")

def sender(id, text,): #функция отправки сообщения от бота
  vk_session.method("messages.send", {"chat_id" : id, "message" : text, "random_id" : 0})

while True: 
  for event in longpoll.listen():
    if event.type == VkBotEventType.MESSAGE_NEW:
      if event.to_me:
        if event.from_chat:
          id = event.chat_id
          user_id = event.object.message['from_id']
          msg = event.object.message['text'].lower()

          if msg == "привет":
            sender(f"@id{user_id}Привет!")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WolfInChains, 2021-08-04
@fodich

Because you need to decide what exactly you want to use.
Here is a long poll for the user (you need Vk Bot LongPoll for the group)
longpoll = VkLongPoll(vk_session)
And here you catch messages for the group
if event.type == VkBotEventType.MESSAGE_NEW:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question