Skip to main content

srai-telegrambot

A library that makes it easier to build ai powered telegram bots

install

pip install srai-telegrambot

get your token

get your telegrambot token from the botfather as described here https://core.telegram.org/bots/tutorial

usage

This is how i use the bot: I rely on mongo db to save messages there are other options! You can also implement your own version of dao telegrambot. I will add a in-memory database that i use for testing in the near future

import os
from srai_core.store.database_mongo import DatabaseMongo
from srai_core.tools_env import get_string_from_env

from srai_telegrambot.command.command_chat_id import CommandChatId
from srai_telegrambot.command.command_help import CommandHelp
from srai_telegrambot.command.command_image_tag import CommandImageTag
from srai_telegrambot.dao_telegram_bot import DaoTelegramBot
from srai_telegrambot.mode.text_mode_gpt import TextModeGpt
from srai_telegrambot.mode.text_mode_rag import TextModeRag
from srai_telegrambot.telegram_bot import TelegramBot

if __name__ == "__main__":
    telegram_token = get_string_from_env("SRAI_TELEGRAM_TOKEN")
    connection_string = get_string_from_env("MONGODB_CONNECTION_STRING")
    telegram_root_id = get_string_from_env(
        "TELEGRAM_ROOT_ID"
    )  # this is my user id and it gives me admin rights in the bot

    database_mongo = DatabaseMongo("database_telegrambot", connection_string)
    dao_telegram_bot = DaoTelegramBot(database_mongo)
    bot = TelegramBot(
        token=telegram_token,
        dao_telegram_bot=dao_telegram_bot,
    )

    bot.register_admin(telegram_root_id)

    # register commands
    bot.register_command(CommandHelp())
    bot.register_command(CommandChatId())
    bot.register_command(CommandImageTag())
    bot.register_text_mode(TextModeGpt("you are a chatbot"), False)

    path_dir_vectorstore = os.path.abspath(os.path.join("test", "data", "vectorstore"))
    bot.register_text_mode(TextModeRag(path_dir_vectorstore), True)
    # start bot
    bot.main()

How to build a voice interface to chatgpt

Below is code for a voice interface to chatgpt

from srai_core.store.database_mongo import DatabaseMongo
from srai_core.tools_env import get_string_from_env

from srai_telegrambot.command.command_chat_id import CommandChatId
from srai_telegrambot.command.command_help import CommandHelp
from srai_telegrambot.command.command_image_tag import CommandImageTag
from srai_telegrambot.dao_telegram_bot import DaoTelegramBot
from srai_telegrambot.mode.voice_mode_gpt import VoiceModeGpt
from srai_telegrambot.telegram_bot import TelegramBot

if __name__ == "__main__":
    telegram_token = get_string_from_env("SRAI_TELEGRAM_TOKEN")
    connection_string = get_string_from_env("MONGODB_CONNECTION_STRING")
    telegram_root_id = get_string_from_env(
        "TELEGRAM_ROOT_ID"
    )  # this is my user id and it gives me admin rights in the bot

    database_mongo = DatabaseMongo("database_telegrambot", connection_string)
    dao_telegram_bot = DaoTelegramBot(database_mongo)
    bot = TelegramBot(
        token=telegram_token,
        dao_telegram_bot=dao_telegram_bot,
    )

    bot.register_admin(telegram_root_id)

    # register commands
    bot.register_command(CommandHelp())
    bot.register_command(CommandChatId())
    bot.register_command(CommandImageTag())
    bot.register_voice_mode(VoiceModeGpt("You are a chatbot. Never respond in more that three sentences"), True)


    # start bot
    bot.main()

How to build a RAG telegram bot vectorstore

Below is code for a rag telegram bot

import os

from srai_telegrambot.mode.text_mode_rag import TextModeRag
from srai_telegrambot.telegram_bot_test import TelegramBotTest

path_dir_vectorstore = os.path.abspath(os.path.join("test", "data", "vectorstore"))
mode = TextModeRag(path_dir_vectorstore)
mode.register(TelegramBotTest())

list_path_file = []
list_path_file.append(os.path.abspath(os.path.join("test", "data", "paper_0.pdf")))
list_path_file.append(os.path.abspath(os.path.join("test", "data", "paper_1.pdf")))
list_path_file.append(os.path.abspath(os.path.join("test", "data", "paper_2.pdf")))

for path_file in list_path_file:
    mode.add_path_file_pdf(path_file)
mode.rebuild_vectorstore()
print(mode._handle_text("test_chat", "What is the most important parameter in DCE-CT in stroke?"))
print(mode._handle_text("test_chat", "Please elaborate?"))

Changelog

0.17.0

  • Added voice support
  • Removed inate vectorstore FAISS

0.16.0

  • Added RAG text mode
  • Moved memory databases to core lib

Metadata

Release files for srai-telegrambot 0.17.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for srai-telegrambot 0.17.2
File Size Uploaded Provenance Actions
srai_telegrambot-0.17.2.tar.gz 9.7 kB Without provenance Details for srai_telegrambot-0.17.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for srai-telegrambot 0.17.2
File Interpreter ABI Platform Actions
srai_telegrambot-0.17.2-py3-none-any.whl Python 3 none any Details for srai_telegrambot-0.17.2-py3-none-any.whl

Total release size: 24.0 kB

Release files / srai_telegrambot-0.17.2.tar.gz

Download URL srai_telegrambot-0.17.2.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d47fd113c1756f45ca5a6c98cdcbed504b33207affba64633b08fcd9642ce336
BLAKE2b-256 checksum
How to use checksums
f5f804ecdc6661c52e75d6cb25899358998de78c793195c0e75e336f0ca245ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.11

Release files / srai_telegrambot-0.17.2-py3-none-any.whl

Download URL srai_telegrambot-0.17.2-py3-none-any.whl
Size 14.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a8f301f1f8bc12b260b7dd9c44c4ff44bd1f7130fd35549f3698c83baaaef539
BLAKE2b-256 checksum
How to use checksums
b6e52840119cbcf052fb59e2fb42216ccb725ebb3d23bcdab26f0aa1a4aeeecc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.11

Security for srai-telegrambot 0.17.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page