Skip to content

Commit

Permalink
Add redis_url to StateRedisStorage to allow initialization by URL
Browse files Browse the repository at this point in the history
Useful for rediss:// and unix:// schemes
  • Loading branch information
Cub11k committed Sep 8, 2023
1 parent cb5b70d commit 24ec125
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions telebot/asyncio_storage/redis_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ class StateRedisStorage(StateStorageBase):
To use it, just pass this class to:
TeleBot(storage=StateRedisStorage())
"""
def __init__(self, host='localhost', port=6379, db=0, password=None, prefix='telebot_'):
def __init__(self, host='localhost', port=6379, db=0, password=None, prefix='telebot_', redis_url=None):
if not redis_installed:
raise ImportError('AioRedis is not installed. Install it via "pip install aioredis"')

if is_actual_aioredis:
aioredis_version = tuple(map(int, aioredis.__version__.split(".")[0]))
if aioredis_version < (2,):
raise ImportError('Invalid aioredis version. Aioredis version should be >= 2.0.0')
self.redis = aioredis.Redis(host=host, port=port, db=db, password=password)
if redis_url:
self.redis = aioredis.Redis.from_url(redis_url)
else:
self.redis = aioredis.Redis(host=host, port=port, db=db, password=password)

self.prefix = prefix
#self.con = Redis(connection_pool=self.redis) -> use this when necessary
Expand Down
7 changes: 5 additions & 2 deletions telebot/storage/redis_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ class StateRedisStorage(StateStorageBase):
To use it, just pass this class to:
TeleBot(storage=StateRedisStorage())
"""
def __init__(self, host='localhost', port=6379, db=0, password=None, prefix='telebot_'):
def __init__(self, host='localhost', port=6379, db=0, password=None, prefix='telebot_', redis_url=None):
super().__init__()
self.redis = ConnectionPool(host=host, port=port, db=db, password=password)
if redis_url:
self.redis = ConnectionPool.from_url(redis_url)
else:
self.redis = ConnectionPool(host=host, port=port, db=db, password=password)
#self.con = Redis(connection_pool=self.redis) -> use this when necessary
#
# {chat_id: {user_id: {'state': None, 'data': {}}, ...}, ...}
Expand Down

0 comments on commit 24ec125

Please sign in to comment.