Home > Net >  How do I transfer the value that the user wrote to another file?
How do I transfer the value that the user wrote to another file?

Time:04-14

I wrote the simplest bot for VK. I have 2 files: one with the bot, the other with the main code. How do I transfer the value (id_user) contained in the bot to another file with the main code? To put it simply, I need to move the 'id_user' variable to another python file. How to do it?

import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType


def write_msg(user_id, message):
    vk.method('messages.send', {'user_id': user_id, 'message': message, 'random_id': 0})

token = 'some_token'

vk = vk_api.VkApi(token=token)

longPoll = VkLongPoll(vk)

for event in longPoll.listen():
    if event.type == VkEventType.MESSAGE_NEW:
        if event.to_me:
            request = event.text
            if '/fake' in request:
                write_msg(event.user_id, 'Wait...')
                if 'https://vk.com/' in request and ' ' in request:
                    id_user = request.split(' ')[1]
                    write_msg(event.user_id, str(id_user))
                else:
                    write_msg(event.user_id, 'Invalid link')
            else:
                write_msg(event.user_id, 'Use the command "/fake {acc_id}"')

I will be very grateful for your help

CodePudding user response:

You can simply import the file you want to use variable from.

import main

or maybe you can just import variable like this:

from main import id_user
  • Related