Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redis_url to StateRedisStorage to allow initialization by URL #2041

Merged
merged 1 commit into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading