-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91dd4c4
commit 384b355
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import json | ||
import logging | ||
from typing import Optional | ||
|
||
import aiohttp | ||
|
||
|
||
async def pre_send(bot, chat_id: Optional[str]=None, action: str='typing') -> None: | ||
""" | ||
Same as Bot.pre_send() but asynchronous | ||
""" | ||
data = { | ||
'chat_id': chat_id if chat_id is not None else self.chat_id, | ||
'action': action | ||
} | ||
await call(bot, 'sendChatAction', 'POST', data=data) | ||
|
||
|
||
async def send(bot, chat_id: Optional[str]=None, text: Optional[str]=None, data: dict={}) -> None: | ||
""" | ||
Same as Bot.send() but asynchronous | ||
""" | ||
if not text: | ||
return | ||
data.update(chat_id=chat_id if chat_id is not None else bot.chat_id, text=text) | ||
logging.debug('modules.utils.http.send :: Sending: {}'.format(data)) | ||
await call(bot, 'sendMessage', 'POST', data=data) | ||
|
||
|
||
async def call(bot, method_name: str, http_method: str, **kwargs): | ||
""" | ||
Same as Bot.call() but asynchronous | ||
""" | ||
|
||
timeout = getattr(bot.settings, 'updates_timeout', 60) | ||
|
||
uri = 'https://api.telegram.org/bot{}/{}'.format( | ||
bot.token, method_name) | ||
session = aiohttp.ClientSession() | ||
try: | ||
with aiohttp.ClientSession() as session: | ||
async with session.request(method=http_method, url=uri, **kwargs) as response: | ||
logging.debug('modules.utils.http.call :: url: {}, status: {}'.format(uri, response.status)) | ||
response_text = await response.text() | ||
except Exception as e: | ||
logging.debug('modules.utils.http.call :: got exception: {}'.format(e)) | ||
return | ||
|
||
logging.debug('modules.utils.http.call :: response: {}'.format(response_text)) | ||
return json.loads(response_text) | ||
|
||
|
||
async def perform_request(url, method, params={}): | ||
try: | ||
with aiohttp.ClientSession() as session: | ||
async with session.request(method=method, url=url, params=params) as response: | ||
logging.debug('modules.utils.http.make_request :: url: {}, status: {}'.format(url, response.status)) | ||
response_text = await response.text() | ||
except Exception as e: | ||
logging.debug('modules.utils.http.make_request :: got exception: {}'.format(e)) | ||
return | ||
return response_text | ||
|