Всех приветствую, делаю бота на aiogram. Из-за специфического предназначения бота, его функционал должен иметь два режима работы "отправка сообщений целиком" и "отправка с эффектом печати". Для этого было решено разделить код на три файла main.py, func1.py, func2.py. В main.py пользователь посредством Inline кнопок должен выбрать режим работы бота.
К проблеме. Из-за малого опыта, я пока не очень догоняю, как можно из одного файла запустить код бота в другом. Долго гуглил, но так и не сообразил, как это сделать.
main.py
from aiogram import Bot, types, asyncio
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor, exceptions
import config
import scenario
import markups as mark
from time import sleep as sl
from config import TOKEN
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
@dp.message_handler(commands = ['start'])
async def choice_effect(mes):
choice_button=InlineKeyboardMarkup(row_width = 1)
One = InlineKeyboardButton("Начать выполнение кода из func1.py", callback_data= "choice_one")
choice_button.add(One)
await bot.send_message(mes.chat.id, "Нажмите на кнопку для запуска кода из func1.py", reply_markup = choice_button)
@dp.callback_query_handler(text_startswith="choice_")
async def call_back0(call: types.CallbackQuery):
if call.data == 'choice_one':
#После соблюдения условия должен выполняться код из func1.py
if __name__ == "__main__":
executor.start_polling(dp, skip_updates = True)
Файл с кодом для постепенного вывода сообщений.
func1.py
from aiogram import Bot, types, asyncio
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor, exceptions
import config
import scenario
import markups as mark
from time import sleep as sl
from config import TOKEN
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
f = list(open ('file_with_text.txt', encoding = 'Windows-1251'))
@dp.message_handler("Пока не знаю, что сюда передать")
async def start(message: types.Message):
for i, line in enumerate(f):
if i < 7:
orig_text = line
msg = await bot.send_message(message.chat.id, 'I')
sl(0.1)
tbp = orig_text[:1]
for x in orig_text[1:]:
try:
await bot.edit_message_text(chat_id=message.chat.id, message_id=msg.message_id, text=f'{tbp}|')
sl(0.1)
tbp += x
await bot.edit_message_text(chat_id=message.chat.id, message_id=msg.message_id, text=tbp)
except exceptions.RetryAfter as e:
await asyncio.sleep(e.timeout)