2

I have a problem with import information from one file to another in python. main.py

from datetime import timedelta, datetime, timezone

import time
import telebot
bot = telebot.TeleBot("<MY-BOT-TOKEN>")

message_list = {
    '1': 'First day',
    '2': 'Second day'
}
user_id=1
date_of_start=0

@bot.message_handler(commands=['start'])
def start_every_day_message(message):
    global user_id
    user_id = message.from_user.id
    global date_of_start
    date_of_start = int(datetime.today().day)
    bot.send_message(user_id, 'You start every day spamming')


@bot.message_handler(commands=['stop'])
def stop_every_day_message(message):
    bot.send_message(user_id, "You stop your every day spamming")
    global date_of_start
    print(date_of_start, user_id)
    date_of_start = 0


if __name__ == '__main__': 
    try:
        bot.polling(none_stop=True)
    except Exception as e:
       print(e) 
       time.sleep(15)

messsage_sender.py

from main import user_id, date_of_start,bot,message_list
from datetime import datetime
print(user_id)
this_day = int(datetime.today().day)
current_diferience = 0

if this_day - date_of_start < 0:
    if (int(datetime.today().month) - 1) == 0 or (int(datetime.today().month) - 1) == 1 or (
            int(datetime.today().month) - 1) == 3 or (int(datetime.today().month) - 1) == 5 or (
            int(datetime.today().month) - 1) == 7 or (int(datetime.today().month) - 1) == 8 or (
            int(datetime.today().month) - 1) == 10:
        current_diferience = this_day - date_of_start + 31
    elif (int(datetime.today().month) - 1) == 2 and (int(datetime.today().year) % 4 == 0):
        current_diferience = this_day - date_of_start + 29
    elif (int(datetime.today().month) - 1) == 2 and (int(datetime.today().year) % 4 != 0):
        current_diferience = this_day - date_of_start + 28
    else:
        current_diferience = this_day - date_of_start + 30
else:
    current_diferience = this_day - date_of_start
if current_diferience <= 30:
    bot.send_message(user_id, message_list.get(str(current_diferience), 0))
else:
    bot.send_message(user_id, "First day")

I have to use some arguments from main.py in message_sender.py . I have a server and main is working all the time and at the first time after /start it should write some info and save it. And it does it every time. After that I daily start my second script and send a message using user_id and date_of_start from main.py. However, when I use import function, it reruns code and user_id and date_of_start get their basic values. How can I fix it or send info in the different way?

0stone0
  • 34,288
  • 4
  • 39
  • 64
Mr. Shiz
  • 21
  • 4
  • You've placed your **private BOT token** in the question. I removed it, but it's still visible in the history. Consider renewing the token at @botFather to prevent other people abusing it! – 0stone0 Sep 28 '21 at 12:56
  • `import` loads values from script, not current values from variables in memory so it can't get current values. Maybe you should save these values in file and other script should read from file. OR maybe in bot you should use something like [jobqueue](https://python-telegram-bot.readthedocs.io/en/stable/telegram.ext.jobqueue.html) to run code periodically - and you wouldn't have to do it manually. See also [How to send message in specific time TelegramBot](https://stackoverflow.com/questions/48288124/how-to-send-message-in-specific-time-telegrambot) – furas Sep 28 '21 at 14:53

0 Answers0