Skip to main content

A simple and lightweight Telegram bot framework

Project description

osonbot

A simple Telegram bot framework.

Installation

pip install osonbot

Example echo bot

from osonbot import Bot

bot = Bot("YOUR_BOT_TOKEN")

bot.when("/start", "Hello {first_name}!")
bot.when("*", "{message_text}")

bot.run()

Documentation

Handling messages

bot.when("MESSAGE_YOU_WANT_TO_HANDLE", "TEXT_YOU_WANT_TO_SEND_WHEN_MESSAGE_IS_HANDLED")

example

bot.when("/start", "Hello {first_name}")

you can give function to do if you want to do multiple task

from osonbot import Bot

bot = Bot("YOUR_BOT_TOKEN")

def greet(msg):
    # task 1
    # task 2
    return "All the tasks are done"

bot.when("/do_task", greet)

if you run this code above, you will get "All the tasks are done" message from your telegram bot

since when method returns self, you can write everything in one line like this. Example in echo bot

from osonbot import Bot

Bot("YOUR_BOT_TOKEN").when("/start", "Hello, {first_name}").when("/help", "How can i help you").when("{message_text}")

first_name will be formatted as the telegram users real first name automatically here are the texts that the osonbot formats automatically:

  • first_name: telegram user's first name
  • last_name: telegram user's last name
  • full_name: telegram user's full name
  • message_text: message's text sent by user
  • user_id: telegram user's id
  • message_id: message's id sent by user

examples on these

bot.when("/firstname", "your first name is {first_name}")
bot.when("/fullname", "your full name is {full_name}")
bot.when("/lastname", "your last name is {last_name}")
bot.when("/text", "you sent {message_text}")
bot.when("/id", "your telegram id is {user_id}")
bot.when("/msgid", "your message's id is {message_id}")

Handling Medias

to handle medias, use when method, but instead of putting string like this when("photo") or when("video"), use osonbot's Photo or Video objects

example

from osonbot import Bot, Photo, Video, Audio, Voice, Sticker

bot = Bot("BOT_TOKEN")

bot.when(Photo, "you sent a photo")
bot.when(Video, "you sent a video")
bot.when(Audio, "you sent a Audio")
bot.when(Voice, "you sent a Voice")
bot.when(Sticker, "you sent a Sticker")

Sending Buttons

Sending keyboard buttons

To send reply keyboard buttons, use osonbot's KeyboardButton function. Set KeyboardButton to reply_markup attribute in bot.when

from osonbot import Bot, KeyboardButton

bot = Bot("YOUR_BOT_TOKEN")

row1 = ["Button 1", "Button 2"]
row2 = ["Button 3"]
row3 = ["Button 4", "Button 5"]

bot.when("hi", "hi, {first_name}\nchoose", reply_markup=KeyboardButton(row1, row2, row3))

you can give buttons as many as you want. KeyboardButton has resize_keyboard attribute which is True by default and one_time_keyboard which is False by default.

Sending inline keyboard buttons

To send inline keybaord buttons, use osonbot's InlineKeyboardButton function. Set InlineKeyboardButton function to reply_markup attribute in bot.when

InlineKeyboardButton Usage

InlineKeyboardButton(
    [["BUTTON_TEXT", "BUTTON_CALLBACK_DATA"], ["BUTTON_TEXT", "BUTTON_CALLBACK_DATA"]],
    [same here]
)

Example

from osonbot import Bot, InlineKeyboardButton

bot = Bot("YOUR_BOT_TOKEN")

row1 = [["Button 1", "btn1"], ["Button 2", "btn2"]]
row2 = [["Button 3", "btn3"]]

bot.when("buttons", "choose buttons", reply_markup=InlineKeyboardButton(row1, row2))

Sending inline keyboard buttons, while setting url to them

To do this, use URLKeyboardButton from osonbot. Its usage is same as InlineKeyboardButton, but instead of putting callback_data, put valid url

from osonbot import Bot, URLKeyboardButton

bot = Bot("YOUR_BOT_TOKEN")

row1 = [["Github", "https://github.com/"], ["Google", "https://google.com"]]
row2 = [["YouTube", "https://www.youtube.com"]]

bot.when("buttons", "choose buttons", reply_markup=URLKeyboardButton(row1, row2))

Remove Keyboard Buttons

To remove it, import RemoveKeyboardButton function from osonbot

from osonbot import Bot, RemoveKeyboardButton

bot = Bot("YOUR_BOT_TOKEN")

bot.when("remove", "keyboard buttons are removed", reply_markup=RemoveKeyboardButton())

Automatic Database and Admin Panel

osonbot handles database and admin automatically which means there is a simple built-in admin panel, and it creates database automatically and saves users' username and user ids. To get the data from database, use bot.db.get_data()

bot.db.get_data()

Overwrite the table

by default, osonbot only saves users' usernames and user ids To this and add another columns, use bot.db.create_table() function

bot.db.create_table("TABLE_NAME", username=str, phone_number=int, first_name=str)

osonbot CLI

Run the bot

osonbot run [filename].py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

osonbot-1.0.8.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

osonbot-1.0.8-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file osonbot-1.0.8.tar.gz.

File metadata

  • Download URL: osonbot-1.0.8.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for osonbot-1.0.8.tar.gz
Algorithm Hash digest
SHA256 3341f147c6dc1151f3da282e975e0df1831aaa7e9877321bbb6e937810fde5a8
MD5 2c5471f2c81ea5c1a2eed96bdfe64b86
BLAKE2b-256 384c9a394d0c5a574376392f5d95dd6a6c743411ba9870f1b7520314f009df99

See more details on using hashes here.

File details

Details for the file osonbot-1.0.8-py3-none-any.whl.

File metadata

  • Download URL: osonbot-1.0.8-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for osonbot-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 19f4ab69d196a3ac15b186f19cb9ed347d57681fe44fba1daf9d4bac2c51ab9b
MD5 ea1b30336f0466b9d3ac57909f3da613
BLAKE2b-256 8d4cb0f3312d7ff9008dd8cfdc31bbd3f2ba3381b85d0eb51fe34a8e60ef6023

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page