1
1
import asyncio
2
+ from asyncio import Task
3
+ from typing import Any , Coroutine
2
4
3
5
from loguru import logger
4
6
from telegram import (
38
40
39
41
class HiroshiBot :
40
42
def __init__ (self ) -> None :
43
+ self .background_tasks : set [Task [Any ]] = set ()
41
44
self .commands = [
42
45
BotCommand (command = "about" , description = "About this bot" ),
43
46
BotCommand (command = "help" , description = "Show the help message" ),
@@ -53,6 +56,11 @@ def __init__(self) -> None:
53
56
BotCommand (command = "provider" , description = "Select GPT provider" ),
54
57
]
55
58
59
+ def create_task (self , task : Coroutine [Any , Any , Any ]) -> None :
60
+ task_scheduled = asyncio .create_task (task )
61
+ self .background_tasks .add (task_scheduled )
62
+ task_scheduled .add_done_callback (self .background_tasks .discard )
63
+
56
64
async def help (self , update : Update , context : ContextTypes .DEFAULT_TYPE ) -> None :
57
65
telegram_message = get_telegram_message (update = update )
58
66
commands = [f"/{ command .command } - { command .description } " for command in self .commands ]
@@ -75,7 +83,7 @@ async def about(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> Non
75
83
@check_user_allowance
76
84
@check_user_allow_to_apply_settings
77
85
async def reset (self , update : Update , context : ContextTypes .DEFAULT_TYPE ) -> None :
78
- asyncio .create_task (handle_reset (update = update , context = context ))
86
+ self .create_task (task = handle_reset (update = update , context = context ))
79
87
80
88
@check_user_allowance
81
89
async def prompt (self , update : Update , context : ContextTypes .DEFAULT_TYPE ) -> None :
@@ -93,11 +101,11 @@ async def prompt(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
93
101
and not user_interacts_with_bot (update = update , context = context )
94
102
):
95
103
return None
96
- asyncio .create_task (handle_prompt (update = update , context = context ))
104
+ self .create_task (task = handle_prompt (update = update , context = context ))
97
105
98
106
@check_user_allowance
99
107
async def ask (self , update : Update , context : ContextTypes .DEFAULT_TYPE ) -> None :
100
- asyncio .create_task (handle_prompt (update = update , context = context ))
108
+ self .create_task (task = handle_prompt (update = update , context = context ))
101
109
102
110
@check_user_allowance
103
111
@check_user_allow_to_apply_settings
@@ -109,7 +117,7 @@ async def show_menu(self, update: Update, context: ContextTypes.DEFAULT_TYPE) ->
109
117
110
118
@check_user_allow_to_apply_settings
111
119
async def select_provider (self , update : Update , context : ContextTypes .DEFAULT_TYPE ) -> None :
112
- asyncio .create_task (handle_provider_selection (update = update , context = context ))
120
+ self .create_task (task = handle_provider_selection (update = update , context = context ))
113
121
114
122
@check_user_allowance
115
123
async def inline_query (self , update : Update , context : ContextTypes .DEFAULT_TYPE ) -> None :
@@ -139,8 +147,8 @@ def run(self) -> None:
139
147
app = (
140
148
ApplicationBuilder ()
141
149
.token (telegram_settings .token )
142
- .proxy_url (telegram_settings .proxy )
143
- .get_updates_proxy_url (telegram_settings .proxy )
150
+ .proxy (telegram_settings .proxy )
151
+ .get_updates_proxy (telegram_settings .proxy )
144
152
.post_init (self .post_init )
145
153
.build ()
146
154
)
0 commit comments