This example shows how to program a Telegram Bot that charges for its services using the api payments methods.
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j8
./paid_subscription_bot YOUR_BOT_TOKEN YOUR_PAYMENT_PROVIDER_TOKEN
- Open the Telegram mobile app and search BotFather
- Send BotFather a command /newbot
- Follow instructions to create a new Bot
- After you finish the instructions, you will receive a Bot Token, make sure you keep it secured.
- Open the Telegram mobile app and search BotFather
- Send BotFather a command /mybots
- Click your bot
- Click Payments
- Choose your payment provider and follow instructions to get your payment provider token, make sure you keep it secured.
Never use real money to test your Bot invoice payments. Telegram has a testing card which you can use to pay for your testing invoices:
see Testing payments
Simply use card info:
Card number 42 42 42 42 42 42 42 42
CVV any
Expiration Date any
Using this testing card will perform an invoice payment callback to your bot and will trigger the Bot::onPreCheckoutQuery
, then you will have to
answer to the query in less than 10s with Api::answerPreCheckoutQuery
to confirm or deny the payment.
flowchart TD
User -->|/subscribe| Bot
Bot --> C{User subscribed already?}
C -->|Yes| D[Bot::Ignore]
C -->|No| E[Bot::subscribeNewUser]
E --> Api::sendInvoice --> User
User --> PaysForInvoice
PaysForInvoice-->Bot::onPreCheckoutQuery-->Bot
Bot-->K{Approve or deny payment}
K--> |Approve| F[Api::answerPreCheckoutQuery OK = TRUE]
K--> |Deny| G[Api::answerPreCheckoutQuery OK = FALSE]
F--> Bot::addUserToSubscribersList
G--> Bot::sendDenialErrorMessage