i have created a bot that is runing on main.py with schedule and i have other python file (telegram.py) that receive messages trough telegram to update a flag that is my itention to start and stop the bot in main.py. How can this be achieved? I have tried so many things, but i cant access the STATUS_FLAG from my telegram.py inside my main.py, i always get 0 as value.
My telegram.py file have this content:
import requests
import logging
from aiogram import Bot, Dispatcher, executor, types
TG_TOKEN = "TOKEN"
STATUS_FLAG = 0
# Configure logging
logging.basicConfig(level=logging.INFO)
# Initialize bot and dispatcher
bot = Bot(token=TG_TOKEN)
dp = Dispatcher(bot)
@dp.message_handler(commands=['start', 'stop', 'status', 'help'])
async def send_welcome(message: types.Message):
"""
This handler will be called when user sends `/*` command
"""
global STATUS_FLAG
print(message['text'])
if message['text'] == "/start":
if STATUS_FLAG == 0:
STATUS_FLAG = 1
await message.reply("I'm going to start bot")
else:
await message.reply("Bot is already running")
if message['text'] == "/stop":
if STATUS_FLAG == 1:
STATUS_FLAG = 0
await message.reply("I'm going to stop bot")
else:
await message.reply("Bot is already stoped")
if message['text'] == "/status":
print(STATUS_FLAG)
if STATUS_FLAG == 0:
await message.reply("Bot is stopped")
else:
await message.reply("Bot is running")
if message['text'] == "/help":
text = "/start - Start the bot\n"
text += "/stop - Stop the bot\n"
text += "/status - Get bot status"
await message.reply(text)
@dp.message_handler()
async def echo(message: types.Message):
# old style:
# await bot.send_message(message.chat.id, message.text)
print(message.text)
await message.answer(message.text)
if __name__ == "__main__":
executor.start_polling(dp, skip_updates=True)
Can someone please guide me trough the correct way of doing this? Thank you so much.
I already tried to define a get function inside telegram.py and call it inside main.py
If I understand you correctly, here is the answer. You simply return the flag to the function and output the flag where you need them.
@dp.message_handler(commands=['start', 'stop', 'status', 'help'])
async def send_welcome(message: types.Message):
"""
This handler will be called when user sends `/*` command
"""
global STATUS_FLAG
print(message['text'])
if message['text'] == "/start":
if STATUS_FLAG == 0:
STATUS_FLAG = 1
await message.reply("I'm going to start bot")
else:
await message.reply("Bot is already running")
if message['text'] == "/stop":
if STATUS_FLAG == 1:
STATUS_FLAG = 0
await message.reply("I'm going to stop bot")
else:
await message.reply("Bot is already stoped")
if message['text'] == "/status":
print(STATUS_FLAG)
if STATUS_FLAG == 0:
await message.reply("Bot is stopped")
else:
await message.reply("Bot is running")
if message['text'] == "/help":
text = "/start - Start the bot\n"
text += "/stop - Stop the bot\n"
text += "/status - Get bot status"
await message.reply(text)
return STATUS_FLAG #importantly
Do not forget to import the file
import telegram
@dp.message_handler(commands='list_wins')
async def list_wins_combo(message: types.Message):
print(await telegram.send_welcome(message)) # your file and function
await bot.send_message(message.from_user.id, 'some text', parse_mode='html')