Answer the question
In order to leave comments, you need to log in
Extracting data from a message?
Greetings!
I recently started learning python and the question arose of how to extract text from a message. for example
info text hereand send to userThe probability that the text here is {some number}%
Answer the question
In order to leave comments, you need to log in
import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
from vk_api.utils import get_random_id
import random
vk_session = vk_api.VkApi(
token='TOKENGROUP')
longpoll = VkBotLongPoll(vk_session, 'IDGROUP')
vk = vk_session.get_api()
while True:
try:
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
response = str(event.message.text)
text = str(event.message.text.lower())
peer_id = str(event.message.peer_id)
from_id = str(event.message.from_id)
message = text.split(' ', 10)
if message[0] == '/инфа':
texts = response[5:]
infa = random.randint(1, 100)
vk.messages.send(
peer_id=peer_id,
random_id=get_random_id(),
message=f'Вероятность, что {texts} {infa}%'
)
except Exception as e:
print(e)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question