Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeSkip to content
python-telegram-bot
v22.1
Logo
python-telegram-bot
v22.1

Reference

Resources

Project

Back to top

Examples

In this section we display small examples to show what a bot written withpython-telegram-bot looks like.Some bots focus on one specificaspect of the Telegram Bot API while others focus on one of themechanics of this library. Except for therawapibot.py example, they all use the high-levelframework this library provides with thetelegram.ext submodule.

All examples are licensed under theCC0Licenseand are therefore fully dedicated to the public domain. You can use themas the base for your own bots without worrying about copyrights.

Do note that we ignore one pythonic convention. Best practice woulddictate, in many handler callbacks function signatures, to replace theargumentcontext with an underscore, sincecontext is an unusedlocal variable in those callbacks. However, since these are examples andnot having a name for that argument confuses beginners, we decided tohave it present.

echobot.py

This is probably the base for most of the bots made withpython-telegram-bot. It simply replies to each text message with amessage that contains the same text.

timerbot.py

This bot uses thetelegram.ext.JobQueueclass to send timed messages. The user sets a timer by using/setcommand with a specific time, for example/set30. The bot then setsup a job to send a message to that user after 30 seconds. The user canalso cancel the timer by sending/unset. To learn more about theJobQueue, readthis wiki article.Note: To useJobQueue, you must install PTB viapipinstall"python-telegram-bot[job-queue]"

conversationbot.py

A common task for a bot is to ask information from the user. In v5.0 ofthis library, we introduced thetelegram.ext.ConversationHandlerfor that exact purpose. This example uses it to retrieveuser-information in a conversation-like style. To get a betterunderstanding, take a look at thestate diagram.

conversationbot2.py

A more complex example of a bot that uses theConversationHandler.It is also more confusing. Good thing there is afancy state diagram.for this one, too!

nestedconversationbot.py

An even more complex example of a bot that uses the nestedConversationHandlers. While it’s certainly not that complex thatyou couldn’t built it without nestedConversationHanldlers, itgives a good impression on how to work with them. Of course, there is afancy state diagramfor this example, too!

persistentconversationbot.py

A basic example of a bot store conversation state and user_data overmultiple restarts.

inlinekeyboard.py

This example sheds some light on inline keyboards, callback queries andmessage editing. A wiki site explaining this examples liveshere.

inlinekeyboard2.py

A more complex example about inline keyboards, callback queries andmessage editing. This example showcases how an interactive menu could bebuild using inline keyboards.

deeplinking.py

A basic example on how to use deeplinking with inline keyboards.

inlinebot.py

A basic example of aninlinebot. Don’t forget to enableinline mode with@BotFather.

pollbot.py

This example sheds some light on polls, poll answers and thecorresponding handlers.

passportbot.py

A basic example of a bot that can accept passports. Use in combinationwith theHTML page.Don’t forget to enable and configure payments with@BotFather. Check out thisguideon Telegram passports in PTB.Note: To use Telegram Passport, you must install PTB viapipinstall"python-telegram-bot[passport]"

paymentbot.py

A basic example of a bot that can accept payments. Don’t forget toenable and configure payments with@BotFather.

errorhandlerbot.py

A basic example on how to set up a custom error handler.

chatmemberbot.py

A basic example on how(my_)chat_member updates can be used.

webappbot.py

A basic example of howTelegramWebApps can be used. Use incombination with theHTML page.For your convenience, this file is hosted by the PTB team such that youdon’t need to host it yourself. Uses theiro.js JavaScript library to showcase auser interface that is hard to achieve with native Telegramfunctionality.

contexttypesbot.py

This example showcases howtelegram.ext.ContextTypes can be used tocustomize thecontext argument of handler and job callbacks.

customwebhookbot.py

This example showcases how a custom webhook setup can be used incombination withtelegram.ext.Application.

arbitrarycallbackdatabot.py

This example showcases how PTBs “arbitrary callback data” feature can beused.Note: To use arbitrary callback data, you must install PTB viapipinstall"python-telegram-bot[callback-data]"

Pure API

Therawapibot.py example example uses only the pure, “bare-metal” API wrapper.

On this page

[8]ページ先頭

©2009-2025 Movatter.jp