Skip to main content

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}")

Sending and Receiving 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 sending media

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



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")

bot.when(Document, "document is received")

example receivig media

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



bot = Bot("BOT_TOKEN")



bot.when("/photo", Photo("URL_OR_PATH_TO_PHOTO", caption="optional"))

bot.when("/video", Video("URL_OR_PATH_TO_VIDEO", caption="optional"))

same with Audio, Voice, Sticker, and Document

Editing

messages

parse mode, caption and reply_markup is available in this method

bot.edit_message_text(chat_id, message_id, "new message")

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 with 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())

Handling States

Here is the code

bot.state.register("YOUR_STATE_NAME", states=['FIRST_STATE_NAME', 'SECOND_STATE_NAME', 'THIRD_STATE_NAME'...])



bot.when_state("YOUR_EXISTING_STATE_NAME:FIRST_STATE_NAME", 'something to send back when the state is handled', next_state='YOUR_EXISTING_STATE_NAME:SECOND_STATE_NAME')

Note that next_state attribute is optional, and next_state will trigger the next state that comes after if next_state is not changed. The point is that you can skip some states according your idea.

Automatic Database

osonbot offers is a simple built-in automatic database - it creates database automatically, 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.overwrite_table() function

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

Creating tables

bot.db.create_table("TABLE_NAME", column1=str, column2=int)

Getting a data of a table

to do this use bot.db.get_data() method

bot.db.get_data("TABLE_NAME")

automatic Admin panel

osonbot also automatically creates admin panel on your telegram bot

set your telegram id to admin_id attribute on Bot class like this and send /admin to your bot (more features like setting specific command to open admin panel and managing database better will be added in the future)


bot = Bot('bot_token', admin_id=your_integer_telegram_id)

osonbot CLI

Run the bot

cli tool can run your python file, while watching it, and if it spots any change it will rerun it, so you don't need to stop and rerun it manually

osonbot [filename].py

Userbot is coming in later updates

Other Telegram Bot API features will be added in the future

Metadata

Release files for osonbot 1.2.9

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

Source distribution (sdist)

Source distribution for osonbot 1.2.9
File Size Uploaded
osonbot-1.2.9.tar.gz 23.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for osonbot 1.2.9
File Interpreter ABI Platform
osonbot-1.2.9-py3-none-any.whl Python 3 none any Details

Total release size: 43.7 kB

Release files / osonbot-1.2.9.tar.gz

Download URL osonbot-1.2.9.tar.gz
Size 23.5 kB
Tags Source
SHA-256 checksum
How to use checksums
455d3b6db45bd224172035dd67488641f66f3f919d764400c50f3af3a2c51975
BLAKE2b-256 checksum
How to use checksums
941243709366fe8ec7792b0ea46396795ac145880034859093e0b464ca060b36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.0

Release files / osonbot-1.2.9-py3-none-any.whl

Download URL osonbot-1.2.9-py3-none-any.whl
Size 20.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
26feeaa497dcfb61d5d6c866f0fc7a2419ab7ccdefd6527876573ac9c38db32d
BLAKE2b-256 checksum
How to use checksums
a87916d67558435264a7bc860424cf5a98e73f4bc6bcf47c565ccdbb69b0f46a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.0

Release history Release notifications | RSS feed

This release

1.2.9 This release

2 release files

1.2.8

2 release files

1.2.6

2 release files

1.2.5

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.9

2 release files

1.1.8

2 release files

1.1.7

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

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