A) INSTALLATION :
sudo apt update
sudo apt install redis-server
……..
B) Utilisation :
En ligne de commande : redis-cli
PING réponds PONG
Avec Python :
$ python -m pip install redis
>>> import redis
>>> r = redis.Redis()
>>> r.mset({"Croatia": "Zagreb", "Bahamas": "Nassau"})
True
>>> r.get("Bahamas")
b'Nassau'
>>> r.get("Bahamas").decode("utf-8")
'Nassau'
>>>
Pour supprimer les données :
Run the FLUSHALL command to clear all the data in the instance.
Run the FLUSHDB command to clear the data in the currently selected DB.
Planification BOT :
##################
# https://medium.com/analytics-vidhya/python-telegram-bot-with-scheduled-tasks-932edd61c534
#################
from telegram.ext import Updater, CommandHandler, CallbackContext
import logging
import redis
import datetime
###########################################################
r = redis.Redis()
###########################################################
updater = Updater(token='5220124996:AAG1sPY5AFmwXZooKUBLBQc1Dqi5v5ljeUY', use_context=True)
dispatcher = updater.dispatcher
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
###########################################################
j = updater.job_queue
###########################################
def once(context: CallbackContext):
message = "Hello, this message will be sent only once"
# send message to all users
db_keys = r.keys(pattern="*")
for single in db_keys:
idc = r.get(single).decode("UTF-8")
context.bot.send_message(chat_id=idc, text=message)
j.run_once(once, 30)
##################################################
def morning(context: CallbackContext):
message = "Good Morning! Have a nice day!"
# send message to all users
db_keys = r.keys(pattern="*")
for single in db_keys:
idc = r.get(single).decode("UTF-8")
context.bot.send_message(chat_id=idc, text=message)
job_daily = j.run_daily(morning, days=(0, 1, 2, 3, 4, 5, 6), time=datetime.time(hour=7, minute=30, second=00))
###################################################
# writting functionality of the command
def start(update, context):
message = 'Welcome to the bot'
user_id = update.message.from_user.id
user_name = update.message.from_user.name
print(user_id)
print(user_name)
r.set(user_name, user_id)
context.bot.send_message(chat_id=update.effective_chat.id, text=message)
# give a name to the command and add it to the dispaatcher
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
###########################################################
# echo the word entered
def echo(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text)
from telegram.ext import MessageHandler, Filters
echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
dispatcher.add_handler(echo_handler)
updater.start_polling()
Liste des clefs :
import redis
r = redis.Redis()
db_keys = r.keys(pattern="*")
print((len(db_keys)))
for single in db_keys:
chat_id = r.get(single).decode("UTF-8")
print(single.decode("UTF-8"), ": ", chat_id)
Références :
https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-redis-on-debian-10
https://realpython.com/python-redis/
https://techblog.geekyants.com/implementing-redis-on-flutter-applications