|
1 | 1 | # Python TelegramBots |
2 | 2 |
|
3 | 3 | A pure and modern python package to communicate with telegram bot api. |
| 4 | + |
| 5 | +## About |
| 6 | + |
| 7 | +This is an async wrapper over telegram bot api written using python ( + 3.10 ). |
| 8 | + |
| 9 | +This package tries to be close to the bot api interface and there are almost |
| 10 | +nothing more than it. Of course the package is extendable and other features |
| 11 | +can be installed separately. |
| 12 | + |
| 13 | +- All objects and methods ( 6.0 ) are implemented in python using `dataclass`es. |
| 14 | + |
| 15 | +- The serialization stuff are done to convert api python objects to json-like (`dict`, `list`) objects and then json-string and reverse. |
| 16 | + |
| 17 | +- A simple and async http client is available using aiohttp to send requests |
| 18 | + |
| 19 | +_This package contains no full-featured client or bound methods! there're only some classes and a client._ |
| 20 | + |
| 21 | +## How to |
| 22 | + |
| 23 | +All api methods are available under following namespace: |
| 24 | + |
| 25 | +```py |
| 26 | +telegrambots.wrapper.types.methods |
| 27 | +``` |
| 28 | + |
| 29 | +And for objects |
| 30 | + |
| 31 | +```py |
| 32 | +telegrambots.wrapper.types.methods |
| 33 | +``` |
| 34 | + |
| 35 | +### Make a request |
| 36 | + |
| 37 | +You can use our async client to make requests. |
| 38 | + |
| 39 | +```py |
| 40 | +import asyncio |
| 41 | + |
| 42 | +from src.telegrambots.wrapper import TelegramBotsClient |
| 43 | +from src.telegrambots.wrapper.types.methods import SendMessage |
| 44 | +from src.telegrambots.wrapper.types.objects import ( |
| 45 | + InlineKeyboardButton, |
| 46 | + InlineKeyboardMarkup, |
| 47 | +) |
| 48 | + |
| 49 | + |
| 50 | +async def main(): |
| 51 | + client = TelegramBotsClient("BOT_TOKEN") |
| 52 | + |
| 53 | + message = await client( |
| 54 | + SendMessage( |
| 55 | + 123456789, |
| 56 | + "Here is a message from python-telegrambots", |
| 57 | + reply_markup=InlineKeyboardMarkup( |
| 58 | + InlineKeyboardButton.with_url( |
| 59 | + "Repo", "https://github.com/python-telegrambots" |
| 60 | + ) |
| 61 | + ), |
| 62 | + ) |
| 63 | + ) |
| 64 | + |
| 65 | + print(message.message_id) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + asyncio.run(main()) |
| 70 | + |
| 71 | +``` |
| 72 | + |
| 73 | +_Everything is documented and well type hinted._ |
| 74 | + |
| 75 | +Send a file or even multiple files ( local or online ). |
| 76 | + |
| 77 | +```py |
| 78 | +import asyncio |
| 79 | +from pathlib import Path |
| 80 | + |
| 81 | +from src.telegrambots.wrapper import TelegramBotsClient |
| 82 | +from src.telegrambots.wrapper.types.methods import SendMediaGroup |
| 83 | +from src.telegrambots.wrapper.types.objects import InputFile, InputMediaPhoto |
| 84 | + |
| 85 | + |
| 86 | +async def main(): |
| 87 | + client = TelegramBotsClient("BOT_TOKEN") |
| 88 | + |
| 89 | + file_path_1 = Path(__file__).parent.resolve().joinpath("test_photo_1.jpg") |
| 90 | + file_path_2 = Path(__file__).parent.resolve().joinpath("test_photo_2.jpg") |
| 91 | + |
| 92 | + with InputFile(file_path_1) as file: |
| 93 | + with SendMediaGroup( |
| 94 | + 123456789, |
| 95 | + [ |
| 96 | + InputMediaPhoto(file, "My first photo"), |
| 97 | + InputMediaPhoto(InputFile(open(file_path_2, "rb"), "test_photo_2.jpg")), |
| 98 | + InputMediaPhoto("https://imgur.com/t/funny/MpMGFRQ"), |
| 99 | + ], |
| 100 | + ) as send_media_group: |
| 101 | + result = await client(send_media_group) |
| 102 | + |
| 103 | + print(result.__len__()) |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + asyncio.run(main()) |
| 108 | +``` |
| 109 | + |
| 110 | +Send multiple requests using one `aiohttp.ClientSession`. |
| 111 | + |
| 112 | +``` py |
| 113 | +import asyncio |
| 114 | + |
| 115 | +from src.telegrambots.wrapper import TelegramBotsClient |
| 116 | +from src.telegrambots.wrapper.types.methods import ( |
| 117 | + SendMessage, |
| 118 | + EditMessageText, |
| 119 | + DeleteMessage, |
| 120 | +) |
| 121 | + |
| 122 | + |
| 123 | +async def main(): |
| 124 | + client = TelegramBotsClient("BOT_TOKEN") |
| 125 | + |
| 126 | + async with client: |
| 127 | + |
| 128 | + message = await client( |
| 129 | + SendMessage(12345678, "It's a long time that want to say ... I love you.") |
| 130 | + ) |
| 131 | + |
| 132 | + edited_message = await client( |
| 133 | + EditMessageText( |
| 134 | + chat_id=message.chat.id, |
| 135 | + message_id=message.message_id, |
| 136 | + text="Oh sorry, how are you today??!", |
| 137 | + ) |
| 138 | + ) |
| 139 | + |
| 140 | + assert edited_message |
| 141 | + |
| 142 | + await client( |
| 143 | + DeleteMessage( |
| 144 | + chat_id=edited_message.chat.id, message_id=edited_message.message_id |
| 145 | + ) |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + asyncio.run(main()) |
| 151 | + |
| 152 | +``` |
| 153 | + |
| 154 | +Print things! usual way or pretty. |
| 155 | + |
| 156 | +```py |
| 157 | +import asyncio |
| 158 | + |
| 159 | +from src.telegrambots.wrapper import TelegramBotsClient |
| 160 | +from src.telegrambots.wrapper.types.methods import SendMessage |
| 161 | + |
| 162 | + |
| 163 | +async def main(): |
| 164 | + client = TelegramBotsClient("BOT_TOKEN") |
| 165 | + |
| 166 | + async with client: |
| 167 | + |
| 168 | + message = await client(SendMessage(12345678, "It's Show Time.")) |
| 169 | + |
| 170 | + # let's see what we got. |
| 171 | + print(message) |
| 172 | + # Message(message_id=134, date=1651407978, chat=Chat(id=106296897, ... |
| 173 | + # ---- A long story here ---- |
| 174 | + |
| 175 | + # make it more clear |
| 176 | + print(message.pretty_str()) |
| 177 | + # { |
| 178 | + # "chat": { |
| 179 | + # "first_name": "A̤̮ʀαՏH", |
| 180 | + # "id": 12345678, |
| 181 | + # "type": "private" |
| 182 | + # }, |
| 183 | + # "date": 1651407978, |
| 184 | + # "from": { |
| 185 | + # "first_name": "TelegramBots Test", |
| 186 | + # "id": 87654321, |
| 187 | + # "is_bot": true, |
| 188 | + # "username": "TestBot" |
| 189 | + # }, |
| 190 | + # "message_id": 134, |
| 191 | + # "text": "It's Show Time." |
| 192 | + # } |
| 193 | + |
| 194 | +if __name__ == "__main__": |
| 195 | + asyncio.run(main()) |
| 196 | +``` |
| 197 | + |
| 198 | +## Install |
| 199 | + |
| 200 | +_The preview package is available at [PYPI](https://pypi.org/project/telegrambots)._ |
0 commit comments