Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用任务中的ID访问FSM上下文变量_Python_Chatbot - Fatal编程技术网

Python 使用任务中的ID访问FSM上下文变量

Python 使用任务中的ID访问FSM上下文变量,python,chatbot,Python,Chatbot,我一直在研究一个程序电报机器人。我想知道是否可以从periodic函数使用自己的ID更改特定用户的上下文变量(在本例中,我想在60秒后更改它,仅用于测试) 在我的实际程序中,定期每隔5秒钟从队列中读取一些数据(电报ID、新名称),我希望它能替换原来的数据 谢谢你的帮助 # Testing accesing to contex variables import logging import asyncio # import aiogram.utils.markdown as md # from

我一直在研究一个程序电报机器人。我想知道是否可以从
periodic
函数使用自己的ID更改特定用户的上下文变量(在本例中,我想在60秒后更改它,仅用于测试)

在我的实际程序中,
定期
每隔5秒钟从队列中读取一些数据(电报ID、新名称),我希望它能替换原来的数据

谢谢你的帮助

# Testing accesing to contex variables
import logging
import asyncio

# import aiogram.utils.markdown as md
# from aiogram.types import ParseMode
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Text
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.utils import executor

logging.basicConfig(level=logging.INFO)

API_TOKEN = 'YOUR TOKEN'

bot = Bot(token=API_TOKEN)
storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)


# States
class Form(StatesGroup):
    name = State()  
    print_name = State()  
    temp = State()


@dp.message_handler(commands='start')
async def cmd_start(message: types.Message):
    await Form.name.set()
    await message.reply("Hi there! What's your name?")


@dp.message_handler(state=Form.name)
async def process_name(message: types.Message, state: FSMContext):
    async with state.proxy() as data:
        data['name'] = message.text
    await Form.print_name.set()
    await message.reply("Write anything...\nI will reply your name\n")
    

@dp.message_handler(state=Form.print_name)
async def print(message: types.Message, state: FSMContext):
    async with state.proxy() as data:
        await message.reply("Your name is: "+str(data['name']))
    await Form.print_name.set()


async def periodic(sleep_for):
    while True:
        # HERE I want to change context vars like data['name'] using an specifif user ID
        await asyncio.sleep(sleep_for)
    
    
if __name__ == '__main__':
    loop= asyncio.get_event_loop ()
    loop.create_task (periodic(60))
    executor.start_polling(dp, skip_updates=True)