Giter Club home page Giter Club logo

ptb-firebase-persistence's Introduction

Firebase Persistence for python-telegram-bot

PyPI

This is an implementation of python-telegram-bot BasePersistence class that uses Google Firebase as persistence back-end. This has a very nice advantage of being able to look at your user_data, chat_data, bot_data and convesations real-time using the firebase web app.

Installation

The library obviously requires (but does not install) python-telegram-bot. It also requires and installs officeial library foor Google Firebase: firebase-admin

pip install ptb-firebase-persistence

Usage

Before you start: obtain credentials from firebase

First of all you need to obtain firebase credentials, that look like this:

{
  "type": "service_account",
  "project_id": "YOUR_ID",
  "private_key_id": "YOUR_PRIVATE_KEY",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMII...EwQ=\n-----END PRIVATE KEY-----\n",
  "client_email": "firebase-adminsdk-SOME_STRING@SOME_DOMAIN.iam.gserviceaccount.com",
  "client_id": "11743776666698009",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-SOMES_STRING.iam.gserviceaccount.com"
}

and the firebase database url that looks like this, something like https://YOUR_APP.firebaseio.com

Instantiation

From environment variables (recommended)

Store the database URL in an environment variable FIREBASE_URL and the config as a json string in an environment variable FIREBASE_CREDENTIALS.

After that instantiation is as easy as:

from ptb_firebase_persistence import FirebasePersistence
from telegram.ext import Updater


my_persistence = FirebasePersistence.from_environment()

updater: Updater = Updater(
    'BOT_TOKEN',
    persistence=my_persistence,
    use_context=True,
)

Direct instantiation

You can also just pass the firebase credentials as URL as simple init params:

from ptb_firebase_persistence import FirebasePersistence
from telegram.ext import Updater


my_persistence = FirebasePersistence(database_url='YOUR_DATABASE_URL', credentials='YOUR_CREDENTIALS_DICT')

updater: Updater = Updater(
    'BOT_TOKEN',
    persistence=my_persistence,
    use_context=True,
)

That's it! You can now watch your data change live on Firebase.

ptb-firebase-persistence's People

Contributors

licht1stein avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

oleglr

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.