C
C
Cat_from_russia2020-08-08 15:30:29
Python
Cat_from_russia, 2020-08-08 15:30:29

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 here
and send to user
The probability that the text here is {some number}%

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artur, 2020-08-08
@Cat_from_russia

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)

P
Puma Thailand, 2020-08-08
@opium

Learn more python

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question