Answer the question
In order to leave comments, you need to log in
How to deal with periodic non-sending of messages when using vk_api?
I am new to python programming, I am going through a downloaded course where you need to make a bot that responds on behalf of a group using Python and the vk_api library. I ran into a problem that if I just write
self.rani = random.randint(0, 2 * 20)
jj = self.g.messages.send(peer_id=chel, random_id=self.rani, message="What's your name?" )
then every message out of twenty is sent, everything works fine.
And if I want to write a message to the user in response to his message, something like this
class Bot:
def __init__(self,grid,token):
self.grid=grid
self.token=token
self.vk=vk_api.VkApi(token=self. token)
self.vkblp=vk_api.bot_longpoll.VkBotLongPoll(vk=self.vk, group_id=self.grid)
self.g=self.vk.get_api()
def run(self):
for b in self.vkblp.listen():
if b.type==vk_api.bot_longpoll.VkBotEventType.MESSAGE_NEW:
chel = b.message["from_id"]
texts = b.message["text"]
self.rani = random.randint(0, 2 * 20)
print( "hello")
jj=self.g.messages.send(peer_id=chel,random_id=self.rani,message="What's your name?"),
then one message out of two or three is sent, if I make a more complex structure of the bot's answers, then 40 percent is also sent. Moreover, when the message is not sent to the VKontakte user, the bot still sees that the original message came from the user, and the program follows the sending path and prints print("hello") to the console, it's just that (sometimes) this is not followed by sending a message to the user. Messages from the user do not come often (once every few seconds) Is there a problem with self.vkblp.listen() and try the same but without a library with a call to the VKontakte api directly?
Problem solved!
The solution was to replace sending messages from the option that I used:
vk=vk_api.VkApi(token=self.token)
g=self.vk.get_api()
g.messages.send(.......)
to the option suggested by RINAMI:
vk=vk_api.VkApi(token=self.token)
vk.method("messages.send", {....})
Everything else worked for me right away without any problems, thank you very much for your help.
Answer the question
In order to leave comments, you need to log in
I didn’t really understand what you want, but in order to send a message on behalf of a group to a user, you need him to write to you first, and so, here’s a simple bot script I wrote for you:
import vk_api
token = ""
id_group = айди_группы
vk = vk_api.VkApi(token=token)
longpoll = VkBotLongPoll(vk, id_group)
getting_api = vk.get_api()
def otpravitel(id, message):
vk.method("messages.send", {"user_id": id, "message": text, 'random_id': 0})
while True:
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
if event.from_user:
id = event.object.message['from_id']
mes = event.object.message['text']
if mes == 'Привет, как дела?'
otpravitel(id, 'Привет, отлично, у тебя?')
else:
otpravitel(id, 'Я не понял вас')
continue
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question