Answer the question
In order to leave comments, you need to log in
How to make the VK chatbot in python send a message about this by reposting it to the conversation when a new post appears in the group?
I made a chatbot for a conversation on VK, and decided to add the ability for it to send a repost of this post and a message like “A new post has appeared in the group!” to the conversation when a new post appears in a certain group! But I didn't understand how exactly to implement it.
Here is the code:
import vk_api
import random
import pyowm
from pyowm import OWM
from pyowm.utils import config
from pyowm.utils import timestamps
from pyowm.utils.config import get_default_config
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
config_dict = get_default_config()
config_dict['language '] = 'ru'
owm = OWM('PYOWM API TOKEN' , config_dict)
mgr = owm.weather_manager()
observation = mgr.weather_at_place('Moscow')
w = observation.weather
temp = w.temperature('celsius')['temp']
try:
import xyz
except:
pass
from vk_api.utils import get_random_id
def write_message(chat, message) :
authorize.method('messages.send', {'chat_id': chat, 'message': message, 'random_id': get_random_id()})
token = "BOT TOKEN API"
authorize = vk_api.VkApi(token = token)
longpoll = VkBotLongPoll(authorize, group_id = "GO TO GROUPS HERE")
getting_api = authorize.get_api()
write_message(1, "Bot is running!"), print("Bot is running!")
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW and event.from_chat and event.message.get('text'):
reseived_message = event.message.get('text')
chat = event.chat_id
from_id = event.message.get( 'from_id')
if reseived_message == "!info":
write_message(chat, "Hi, I'm a bot , I was created to make your chatting easier with various utilities and built-in programs. To call the commands for which I'm responsible, enter an exclamation mark and without spaces, the command word, for example, "!commands". And you know, I don't follow your correspondence)))"), print("A message was sent under the command !info to chat #" + str(chat) + " for user id" + str(from_id))
elif reseived_message == "!commands":
write_message(chat, "List of bot commands: !commands!info !weather"), print("Message sent under command !command to chat #" + str(chat) + " for user id" + str(from_id))
elif reseived_message == "!weather":
write_message(chat, "Mosca weather: now " + str(temp) + "° " + str(w.detailed_status) + ""), print("Message sent under command !weather in chat: #" + str(chat))
else:
print("Skip in chat #" + str(chat) + " from user id" + str(from_id))
Answer the question
In order to leave comments, you need to log in
In the check loop, for example, there was a post with number 1 in the group, the loop checks (for example, every second), if the post with number 1 does not converge (new post), then the last post is taken, and in the messages.send method, the attachments parameter is passed post id and sent to the right place
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question