aiogram:如何分组、格式化和发送多个词典
aiogram: How to group, format and send multiple dictionaries
您需要为不同的日期发送单独的消息时间表。
这是一个示例输入:
raspisanie = [{'week_day': 'Monday, 'name': 'Lesson 1', 'time': '8:30'}, {'week_day': 'Tuesday', name: 'Lesson 1', 'time': '8:30'}, {'week_day': 'Tuesday', name: 'Lesson 2', 'time': '10:30'}])
我不知道怎么每天发一条短信,变成这样:
第一条短信:
周一
第一课
8:30
第二条短信:
周二
第一课
8:30
第 2 课
10:30
只想到message.answer里面的循环,但这是不可能的。
我很乐意提供任何帮助)
为了更好的使用,最好将week_day保存为索引。 Monday = 0, Tuesday = 1, Wednesday = 2, ... , Sunday = 6
。如果你还想把它当作一个单词,你可以写一个函数把星期几的名字转换成索引,不过这没什么大不了的。
import datetime
import asyncio
def get_schedule_for_today(schedule: list, current_day: int) -> list:
schedule_for_today = []
for lesson in schedule:
if lesson['week_day'] == current_day:
schedule_for_today.append(lesson)
return schedule_for_today
async def daily_worker():
schedule = [{'week_day': 0, 'name': 'Lesson 1', 'time': '8:30'},
{'week_day': 1, 'name': 'Lesson 1', 'time': '8:30'},
{'week_day': 1, 'name': 'Lesson 2', 'time': '10:30'}]
schedule_for_today = get_schedule_for_today(schedule, datetime.datetime.today().weekday())
while True:
current_hour_minute = datetime.datetime.now().strftime("%H:%M")
if current_hour_minute == '00:00':
schedule_for_today = get_schedule_for_today(schedule, datetime.datetime.today().weekday())
for lesson in schedule_for_today:
if lesson['time'] == current_hour_minute:
await bot.send_message(chat_id=100, text=f"{lesson['name']}")
await asyncio.sleep(60)
if __name__ == '__main__':
# register new thread to 'executor'
loop = asyncio.get_event_loop()
loop.create_task(daily_worker())
executor.start_polling(dp, loop = loop)
您需要为不同的日期发送单独的消息时间表。 这是一个示例输入:
raspisanie = [{'week_day': 'Monday, 'name': 'Lesson 1', 'time': '8:30'}, {'week_day': 'Tuesday', name: 'Lesson 1', 'time': '8:30'}, {'week_day': 'Tuesday', name: 'Lesson 2', 'time': '10:30'}])
我不知道怎么每天发一条短信,变成这样:
第一条短信: 周一 第一课 8:30 第二条短信: 周二 第一课 8:30 第 2 课 10:30
只想到message.answer里面的循环,但这是不可能的。 我很乐意提供任何帮助)
为了更好的使用,最好将week_day保存为索引。 Monday = 0, Tuesday = 1, Wednesday = 2, ... , Sunday = 6
。如果你还想把它当作一个单词,你可以写一个函数把星期几的名字转换成索引,不过这没什么大不了的。
import datetime
import asyncio
def get_schedule_for_today(schedule: list, current_day: int) -> list:
schedule_for_today = []
for lesson in schedule:
if lesson['week_day'] == current_day:
schedule_for_today.append(lesson)
return schedule_for_today
async def daily_worker():
schedule = [{'week_day': 0, 'name': 'Lesson 1', 'time': '8:30'},
{'week_day': 1, 'name': 'Lesson 1', 'time': '8:30'},
{'week_day': 1, 'name': 'Lesson 2', 'time': '10:30'}]
schedule_for_today = get_schedule_for_today(schedule, datetime.datetime.today().weekday())
while True:
current_hour_minute = datetime.datetime.now().strftime("%H:%M")
if current_hour_minute == '00:00':
schedule_for_today = get_schedule_for_today(schedule, datetime.datetime.today().weekday())
for lesson in schedule_for_today:
if lesson['time'] == current_hour_minute:
await bot.send_message(chat_id=100, text=f"{lesson['name']}")
await asyncio.sleep(60)
if __name__ == '__main__':
# register new thread to 'executor'
loop = asyncio.get_event_loop()
loop.create_task(daily_worker())
executor.start_polling(dp, loop = loop)