Skip to content

Commit

Permalink
Merge pull request #1077 from Badiboy/master
Browse files Browse the repository at this point in the history
Added short live sessions
  • Loading branch information
Badiboy authored Jan 16, 2021
2 parents a758705 + ec8714a commit 3799a1e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
25 changes: 20 additions & 5 deletions telebot/apihelper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import time
import datetime
from datetime import datetime

try:
import ujson as json
Expand Down Expand Up @@ -29,6 +29,7 @@

CONNECT_TIMEOUT = 3.5
READ_TIMEOUT = 9999
SESSION_TIME_TO_LIVE = None # In seconds. None - live forever, 0 - one-time

RETRY_ON_ERROR = False
RETRY_TIMEOUT = 2
Expand All @@ -40,7 +41,21 @@


def _get_req_session(reset=False):
return util.per_thread('req_session', lambda: session if session else requests.session(), reset)
if SESSION_TIME_TO_LIVE:
# If session TTL is set - check time passed
creation_date = util.per_thread('req_session_time', lambda: datetime.now(), reset)
if (datetime.now() - creation_date).total_seconds() > SESSION_TIME_TO_LIVE:
# Force session reset
reset = True
# Save reset time
util.per_thread('req_session_time', lambda: datetime.now(), True)

if SESSION_TIME_TO_LIVE == 0:
# Session is one-time use
return requests.sessions.Session()
else:
# Session lives some time or forever once created. Default
return util.per_thread('req_session', lambda: session if session else requests.sessions.Session(), reset)


def _make_request(token, method_name, method='get', params=None, files=None):
Expand Down Expand Up @@ -713,7 +728,7 @@ def get_method_by_type(data_type):
def kick_chat_member(token, chat_id, user_id, until_date=None):
method_url = 'kickChatMember'
payload = {'chat_id': chat_id, 'user_id': user_id}
if isinstance(until_date, datetime.datetime):
if isinstance(until_date, datetime):
payload['until_date'] = until_date.timestamp()
else:
payload['until_date'] = until_date
Expand Down Expand Up @@ -755,7 +770,7 @@ def restrict_chat_member(
permissions_json = json.dumps(permissions)
payload = {'chat_id': chat_id, 'user_id': user_id, 'permissions': permissions_json}
if until_date is not None:
if isinstance(until_date, datetime.datetime):
if isinstance(until_date, datetime):
payload['until_date'] = until_date.timestamp()
else:
payload['until_date'] = until_date
Expand Down Expand Up @@ -1255,7 +1270,7 @@ def send_poll(
if open_period is not None:
payload['open_period'] = open_period
if close_date is not None:
if isinstance(close_date, datetime.datetime):
if isinstance(close_date, datetime):
payload['close_date'] = close_date.timestamp()
else:
payload['close_date'] = close_date
Expand Down
2 changes: 1 addition & 1 deletion telebot/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Versions should comply with PEP440.
# This line is parsed in setup.py:
__version__ = '3.7.5.u3'
__version__ = '3.7.5.u4'

0 comments on commit 3799a1e

Please sign in to comment.