Home python vk api python bot send different messages to different people while they...

vk api python bot send different messages to different people while they write the same message

Author

Date

Category

I have a bot for VK conversation and a swearing company I need to do if (there are 5 people) Lyokha swore at him, one was added to the variable, and if Matvey swore, then one was added to him and every 24 hours a message was sent how many times he swore. How to do it correctly if there is a possibility, preferably through an if with an explanation. I’m new to this or link to an article with at least a similar question

import random, vk_api, vk
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
from vk_api.utils import get_random_id
import requests
import sys
import time
import threading
import json
vk_session = vk_api.VkApi (token = 'TOKEN')
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
longpoll = VkBotLongPoll (vk_session, 204434733)
vk = vk_session.get_api ()
from vk_api.longpoll import VkLongPoll, VkEventType
Lslongpoll = VkLongPoll (vk_session)
Lsvk = vk_session.get_api ()
def bot ():
  Lsvk.messages.send (
  user_id = 496658805,
  message = "Bot is running!",
  random_id = get_random_id ()
  )
  for event in longpoll.listen ():
    if event.type == VkBotEventType.MESSAGE_NEW:
      if 'BOT' in str (event) or 'Bot' in str (event) or 'bot' in str (event):
        if event.from_chat:
          if event.from_user:
            vk.messages.send (
            key = ('b0409379fb3b81632ee943deb2e99280c53dd520'), # INSERT PARAMETERS
            server = ('https://lp.vk.com/wh204434733'),
            ts = ('1'),
            random_id = get_random_id (),
            message = 'Hello! I am a DDRX5 bot now you can call me by name! ',
chat_id = event.chat_id
            )
bot = threading.Thread (target = bot)
bot.start ()
################################################# ###############
except:
  Lsvk.messages.send (
    user_id = 496658805,
    message = "Bot is disabled :(",
    random_id = get_random_id ()
   )

Here is my whole bot

That’s how I wrote it and the user_id is underlined in red

import random, vk_api, vk
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
from vk_api.utils import get_random_id
import requests
import sys
import time
import threading
import json
from collections import defaultdict
user_by_counter = defaultdict (int)
try:
vk_session = vk_api.VkApi (token = '')
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
longpoll = VkBotLongPoll (vk_session, 204434733)
vk = vk_session.get_api ()
from vk_api.longpoll import VkLongPoll, VkEventType
Lslongpoll = VkLongPoll (vk_session)
Lsvk = vk_session.get_api ()
def bot ():
  Lsvk.messages.send (
  user_id = 496658805,
  message = "Bot is running!",
  random_id = get_random_id ()
  )
  for event in longpoll.listen ():
    if event.type == VkBotEventType.MESSAGE_NEW:
      if 'BOT' in str (event) or 'Bot' in str (event) or 'bot' in str (event):
        if event.from_chat:
          user_by_counter [user_id] + = 1
          vk.messages.send (
            key = 'b0409379fb3b81632ee943deb2e99280c53dd520', # INSERT PARAMETERS
            server = ('https://lp.vk.com/wh204434733'),
            ts = ('1'),
            random_id = get_random_id (),
            message = 'Hello! I am a DDRX5 bot now you can call me by name! ',
chat_id = event.chat_id
            )

Answer 1, authority 100%

At the beginning, after all imports, on a new line, write from collections import defaultdict
and on the second user_by_counter = defaultdict (int)

but what should be in sending a message, or rather how it should look

for event in longpoll.listen ():
      if event.type == VkBotEventType.MESSAGE_NEW:
        if 'Text from user' in str (event):
          if event.from_chat:
            user_id = event.obj.from_id
            user_by_counter [user_id] + = 1
            vk.messages.send (
              key = (''), # INSERT PARAMETERS
              server = (''),
              ts = (''),
              random_id = get_random_id (),
              message = 'reply to user',
              chat_id = event.chat_id
              )

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions