diff --git a/examples/README.md b/examples/README.md index 6f56ab89..643fe56d 100644 --- a/examples/README.md +++ b/examples/README.md @@ -10,13 +10,14 @@ can be freely used as basic building blocks for your own applications without wo Example | Description ---: | :--- -[**hello**](hello.py) | Demonstration of basic API usage -[**echo**](echo.py) | Reply to every private text message +[**hello_world**](hello_world.py) | Demonstration of basic API usage +[**echobot**](echobot.py) | Echo every private text message [**welcome**](welcome.py) | The Welcome Bot in [@PyrogramChat](https://t.me/pyrogramchat) [**history**](history.py) | Get the full message history of a chat [**chat_members**](chat_members.py) | Get all the members of a chat [**dialogs**](dialogs.py) | Get all of your dialog chats -[**inline_bots**](inline_bots.py) | Query an inline bot and send a result to a chat +[**using_inline_bots**](using_inline_bots.py) | Query an inline bot (as user) and send a result to a chat [**keyboards**](keyboards.py) | Send normal and inline keyboards using regular bots [**callback_queries**](callback_queries.py) | Handle queries coming from inline button presses +[**inline_queries**](inline_queries.py) | Handle inline queries [**raw_updates**](raw_updates.py) | Handle raw updates (old, should be avoided) diff --git a/examples/callback_queries.py b/examples/callback_queries.py index 71538eae..f4a87b00 100644 --- a/examples/callback_queries.py +++ b/examples/callback_queries.py @@ -5,12 +5,12 @@ It uses the @on_callback_query decorator to register a CallbackQueryHandler. from pyrogram import Client -app = Client("123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11") +app = Client("my_bot", bot_token="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11") @app.on_callback_query() def answer(client, callback_query): - callback_query.answer('Button contains: "{}"'.format(callback_query.data), show_alert=True) + callback_query.answer("Button contains: '{}'".format(callback_query.data), show_alert=True) app.run() # Automatically start() and idle() diff --git a/examples/chat_members.py b/examples/chat_members.py index 87f8613d..468ac7de 100644 --- a/examples/chat_members.py +++ b/examples/chat_members.py @@ -2,7 +2,7 @@ from pyrogram import Client -app = Client("my_count") +app = Client("my_account") target = "pyrogramchat" # Target channel/supergroup with app: diff --git a/examples/echo.py b/examples/echobot.py similarity index 100% rename from examples/echo.py rename to examples/echobot.py diff --git a/examples/hello.py b/examples/hello_world.py similarity index 100% rename from examples/hello.py rename to examples/hello_world.py diff --git a/examples/inline_queries.py b/examples/inline_queries.py new file mode 100644 index 00000000..c1727fe6 --- /dev/null +++ b/examples/inline_queries.py @@ -0,0 +1,54 @@ +"""This example shows how to handle inline queries. +Two results are generated when users invoke the bot inline mode, e.g.: @pyrogrambot hi. +It uses the @on_inline_query decorator to register an InlineQueryHandler. +""" + +from uuid import uuid4 + +from pyrogram import ( + Client, InlineQueryResultArticle, InputTextMessageContent, InlineKeyboardMarkup, InlineKeyboardButton +) + +app = Client("my_bot", bot_token="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11") + + +@app.on_inline_query() +def answer(client, inline_query): + inline_query.answer( + results=[ + InlineQueryResultArticle( + id=uuid4(), + title="Installation", + input_message_content=InputTextMessageContent( + "Here's how to install **Pyrogram**" + ), + url="https://docs.pyrogram.ml/start/Installation", + description="How to install Pyrogram", + thumb_url="https://i.imgur.com/JyxrStE.png", + reply_markup=InlineKeyboardMarkup( + [ + [InlineKeyboardButton("Open website", url="https://docs.pyrogram.ml/start/Installation")] + ] + ) + ), + InlineQueryResultArticle( + id=uuid4(), + title="Usage", + input_message_content=InputTextMessageContent( + "Here's how to use **Pyrogram**" + ), + url="https://docs.pyrogram.ml/start/Usage", + description="How to use Pyrogram", + thumb_url="https://i.imgur.com/JyxrStE.png", + reply_markup=InlineKeyboardMarkup( + [ + [InlineKeyboardButton("Open website", url="https://docs.pyrogram.ml/start/Usage")] + ] + ) + ) + ], + cache_time=1 + ) + + +app.run() # Automatically start() and idle() diff --git a/examples/keyboards.py b/examples/keyboards.py index 147154a3..1a1140b6 100644 --- a/examples/keyboards.py +++ b/examples/keyboards.py @@ -10,7 +10,7 @@ like send_audio(), send_document(), send_location(), etc... from pyrogram import Client, ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton # Create a client using your bot token -app = Client("123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11") +app = Client("my_bot", bot_token="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11") with app: app.send_message( @@ -33,19 +33,17 @@ with app: reply_markup=InlineKeyboardMarkup( [ [ # First row - InlineKeyboardButton( # Generates a callback query when pressed "Button", - callback_data=b"data" - ), # Note how callback_data must be bytes + callback_data=b"data" # Note how callback_data must be bytes + ), InlineKeyboardButton( # Opens a web URL "URL", url="https://docs.pyrogram.ml" ), ], [ # Second row - # Opens the inline interface - InlineKeyboardButton( + InlineKeyboardButton( # Opens the inline interface "Choose chat", switch_inline_query="pyrogram" ), diff --git a/examples/inline_bots.py b/examples/using_inline_bots.py similarity index 100% rename from examples/inline_bots.py rename to examples/using_inline_bots.py