Sending Invoice Instruction Using Telegram Bot API
In today's world, Telegram bots have become one of the most useful and effective tools for businesses and services. One of the attractive features of Telegram bots is the ability to process payments and sell products using the relevant API for Telegram bots. In this article, I want to teach you how you can send an invoice for your clients using the sendInvoice function.
To send an invoice, you first need to create a Telegram bot and have its token. This token serves as a key for communicating with the Telegram API. Also, you must ensure that your website address is SSL certified so users can easily complete their payments.
Now, let's prepare the overall structure of the invoice. Invoices should include product details, price, and payment type. This information should be sent to the Telegram API so the user can click on the payment link and complete the payment process.
As an example, in the code below, I will create a simple invoice that introduces a product to the client and includes price and product details. After that, I will explain line by line how this process works.
curl -X POST https://api.telegram.org/botYOUR_BOT_TOKEN/sendInvoice \
-d "chat_id=CHAT_ID" \
-d "title=Product Title" \
-d "description=Product Description" \
-d "payload=payload" \
-d "provider_token=PROVIDER_TOKEN" \
-d "currency=IRR" \
-d "prices=[{\"label\": \"Product Price\", \"amount\": 10000}]" \
-d "max_tip_amount=10000" \
-d "suggested_tip_amounts=[1000, 2000, 3000]" \
-d "start_parameter=start" \
-d "photo_url=image_url" \
-d "reply_markup={\"inline_keyboard\":[[{\"text\": \"Pay\", \"callback_data\": \"payment\"}]]]}"
Line by Line Explanation
Line 1:curl -X POST https://api.telegram.org/botYOUR_BOT_TOKEN/sendInvoice
This starts the process by sending a POST request to the Telegram API. Remember to replace
YOUR_BOT_TOKEN
with your actual bot token.Line 2:
-d "chat_id=CHAT_ID"
In this line,
CHAT_ID
should be replaced with the chat identifier of the user to whom the invoice will be sent.Line 3:
-d "title=Product Title"
This defines the title of the product that will be displayed in the invoice.
Line 4:
-d "description=Product Description"
Here, we enter the details related to the product to provide more information to the user.
Line 5:
-d "payload=payload"
This creates a unique identifier for your transaction.
Line 6:
-d "provider_token=PROVIDER_TOKEN"
This specifies the token of the payment provider that is related to the involved payment gateway.
Line 7:
-d "currency=IRR"
This defines the currency unit, for example, IRR.
Line 8:
-d "prices=[{\"label\": \"Product Price\", \"amount\": 10000}]"
In this segment, we specify the product price along with its label.
Line 9:
-d "max_tip_amount=10000"
This determines the maximum amount that can be tipped by the user.
Line 10:
-d "suggested_tip_amounts=[1000, 2000, 3000]"
This specifies suggested tip amounts that the user can choose from.
Line 11:
-d "start_parameter=start"
This defines an exclusive parameter for identifying the specific payment related to the payment processing.
Line 12:
-d "photo_url=image_url"
This specifies the image URL of the product.
Line 13:
-d "reply_markup={\"inline_keyboard\":[[{\"text\": \"Pay\", \"callback_data\": \"payment\"}]]]}"
In this section, we create an interactive button for making payment which the user can click.