Last week I spent developing my first public program, a Telegram bot that works as a Bitcoin wallet and allows you to “throw coins” to other members of group chats and also make external Bitcoin payments to yourself or to others. “Lightning Apps”. I mean that the reader is generally familiar with Bitcoin and Telegram, because I will try to write briefly, without going into details.
A selection of resources about Bitcoin is available at this link , and Telegram is an instant messaging tool for mobile devices and PCs, which allows creating small applications (chat bots) on their platform.
What are the key features of this app?
- It allows you to thank or encourage the interlocutor with real material value, and not just a “virtual like” , this takes electronic communication to a whole new level.
- A real-life example of an application that can financially interact with other applications using an open payment protocol.
- All components of the application are open source projects and it is possible to modify and apply them for their tasks. The application does not use closed and / or commercial solutions, despite the fact that it fits into the sphere of e-commerce, which today is quite difficult to call open.
And how to apply it in practice?
something like this…
or so ...
')
What do these numbers mean? These are
units of bitcoin - satoshi . At stock prices in the middle of 2019, 1000 satoshi is about $ 0.06. Satoshi can be spent online or easily exchanged for national currency. It is for this reason (liquidity) that is not just another
“record in the database” or
“points that you can spend in the stores of our partners,” but the
most real Internet money .
* Immediately I want to note that some words or phrases are not knowingly trying to write in Russian - either I don’t know the contextual translation itself in some cases, or such a translation in my opinion sounds more misleading than correctIt may be asked how to use such an application if there are no bitcoins. They can easily be purchased in small quantities on one of the many exchanges or exchanger, or from a friend Bitcoiner or even get coins from current bot users. Many wallets on the phone offer to buy bitcoins directly "without departing from the cash register."



When bitcoins get hold of (or rather satos-
s (?)),
Run @atomic_tipbot and do / deposit.
In response, the bot will ask how much Satoshi will contribute to the balance and will send a payment account that can be paid with a mobile wallet by scanning the QR code or by pressing the button (
“open in the wallet” ) on the invoice page.



Confirming the payment on your wallet, almost immediately a payment confirmation will be sent by the bot and this will be visually displayed in the account frame.
Up to this point, we have touched on two components of the application - the back-end telegram bot and the cryptocurrency payment processing system (rather, its front-end).
To create a Telegram bot, I used the
Python Telegram Bot . And of course, the
open source bot itself.
As a payment processing system and solving UX-tasks, the choice fell (in fact, there is almost no choice) on
BTCPayServer , to which the
documentation for this link .
If you look for solutions to processing payment events directly, then you should pay attention to the
Electrum CLI manual pages for the so-called. on-chain payments, and for instant micro-payments (Lightning) solutions like
Lightning-charge or
Sparko .
So is it self-hosted or not !?
Yes it is. In my application, I use free BTCPayServer hosting -
BTCPayJungle , but this is absolutely possible and does not require much time to launch my own processing center. However, it is worth remembering that you will need ~ 500GB of Bitcoin (and Litecoin) available on the hard disk for storing blockchains. Computing and network resources will also be required for the synchronization time and, depending on your needs, the necessary “wrappers” for blockchains to make it convenient to interact with them. BTCPayServer does not contain
by-design private keys or any other information that allows you to control your wallet.
For on-chain (ordinary) payments, you must add a so-called. xpub-key wallet in BTCPayServer to generate addresses for receiving (public keys). This makes it possible and safe to use BTCPayServer even in a “shared” form. All these xpub keys and addresses can be easily obtained in good wallets like
Electrum , to further customize the processing.
Everyone who has encountered Bitcoin has heard that bitcoin is slow and transaction costs are expensive, especially when HYIP, so for an instant micro-payment it is worth using (off-chain) transactions of the Lightning Network.
What is this lightning network?
This is something like a bitcoin plug-in that builds on an additional layer of abstraction and allows you to make instant and almost free transactions on the Bitcoin network. All this is possible thanks to cryptographic magic with multi-signatures, often called a
smart contract . There are several implementations of the Lightning Network protocol, I use the
c-lightning implementation in C.
The main difference between on-chain and off-chain (Lightning) payments is how they are stored. All
on-chain transactions are relayed to all network nodes until one of the Bitcoin nodes of the node announces a
new naming block with transactions included in it. (On
Habré there is even
BitFury , they just produce iron for mining). It is for this reason that we call such transactions
on-chain . Such transactions will be recorded in a common blockchain and will remain in it until the end of its existence. Also, such transactions are publicly available and they can be monitored, monitored and analyzed.
In the case of
off-chain (Lightning) transactions, everything happens differently. Each node of the Lightning-network has one
(or more) on-chain addresses - these are the most common Bitcoin addresses from which everything begins. When Bitcoins appear on such an address, it is possible to open a so-called.
lightning channel to another node, which means that using cryptographic techniques, these two nodes have agreed on the availability of funds among themselves. Later, all transactions (both their own and foreign node transactions) that pass through such a channel are saved only on the nodes of the transaction participants (and intermediary nodes). The only transactions that are recorded on the blockchain are transactions about opening or closing a channel. On a more complete scale, such a network of interconnected nodes is obtained which builds thousands of different paths between participants for the “transfusion” of funds from one node to another according to certain strict rules. Virtually all such transactions are stored only in the database file of the nodes themselves. It is for this reason that we call them
off-chain .
The diagram shows the visualization of all application components.

In short:
1) The user sends the command to the bot in
Telegram2) Telegram sends a
python event message to the bot
application3) The
python application sends a request to
BTCPayServer4) BTCPayServer generates incoming BTC and LTC addresses, and also sends an invoice request to a
lightning-charge , which in turn communicates with the
c-lightning daemon. At the exit, we get a beautiful HTML form of payment processing
5) User pays invoice and sees confirmation
6)
BTCPayServer sends
an invoice payment notification (IPN) to the specified callback_url, in our case
callbacks.py (another python application for receiving notifications)
7) According to the received data, the user's balance changes
... and if the user wanted to withdraw funds (coins that he received from others) ...8) A bot's
Python application waits for text or a QR code image, and when valid data is received, the
paylightning.py * utility makes a payment, which initializes the transaction using c-lightning RPC.
* To be fair, it's worth noting that my paylightning.py wrapper is not completely finished and can return a false-negative result in specific scenarios. I recommend to use a well-debugged wrapper in cases close to productionWhat we have? Users of our bot can inside the group chat or directly can transfer
material value without restrictions. And that's not all - thanks to the Lightning Network protocol, which in a sense is a unified and simplified payment network, it is possible to send the same money that was received in a chat to completely third-party applications. For example, Telegram already had the first Bitcoin payment bot - @lntxbot. “Single balance” allows you to use any of the bots, depending on the preferences of a particular group chat (yes, the problem is absolutely contrived and the solution is quite useless, but the point is). Throw balance from one bot to another is worth nothing and takes seconds! It is important to note that the creators of these bots have never been contacted.


Simply send a deposit request message from @lntxbot to @atomic_tipbot and voul'a!
This is no longer just a “transfer of numbers to the database”, but a payment between two different, unrelated databases. Thus, several LApps have already been integrated into the bot, among them:
sat2.io and
lnsms.world .
What does LApp integration mean ?Each
Lightning Network node is essentially a bitcoin wallet. She has two balances: on-chain and off-chain. On-chain balance is needed to open new channels and store funds from closed channels. But the off-chain balance is just what goes between the nodes. The basic requirements for a node are receiving and sending payments, so each node as software has
pay and
invoice functions. I will demonstrate how this happens:
user@ln-node:/ % lightning-cli invoice 1000000000 internal_description external_description { "payment_hash" : "64c0c8f5f8f708b08487ad1376f3d256f92ccb9606987ba395c2b4193efde5a0", "expires_at" : 1558709834, "bolt11" : "lnbc10m1pwdaj72pp5vnqv3a0c7uytppy845fhdu7j2mujejukq6v8hgu4c26pj0hauksqdpqv4u8getjdeskchmyv4ekxunfwp6xjmmwxqyjw5qcqp2rzjqw3qcrp2u3ggke56wrjlstcg76drmfw680cvjum88sl7ja7mpas7xzxz8sqqfecqqyqqqqlgqqqqqqgqjqn8e3ml733dkms2txxldnuwsllwhhkldss73268hdka3e7f083vjhjqurwqrndqd2rcd85pw7vkywnr4dq7yfd59r0g2mw4wzztwr6dcprxzej6" }
We get the “invoice” of the
BOLT11 specification in this case, which is the format of payments in the Bitcoin network (and Litecoin, by the way, too). This is a string containing information about the recipient, transaction amount, request expiration time, etc. Detailed information can be obtained by running decodepay for this line:
user@ln-node:/ % lightning-cli decodepay lnbc10m1pwdaj72pp5vnqv3a0c7uytppy845fhdu7j2mujejukq6v8hgu4c26pj0hauksqdpqv4u8getjdeskchmyv4ekxunfwp6xjmmwxqyjw5qcqp2rzjqw3qcrp2u3ggke56wrjlstcg76drmfw680cvjum88sl7ja7mpas7xzxz8sqqfecqqyqqqqlgqqqqqqgqjqn8e3ml733dkms2txxldnuwsllwhhkldss73268hdka3e7f083vjhjqurwqrndqd2rcd85pw7vkywnr4dq7yfd59r0g2mw4wzztwr6dcprxzej6 { "currency" : "bc", "created_at" : 1558105034, "expiry" : 604800, "payee" : "025a14b8ed40583d67aec92da19453e0b2d1fbbf75f96f85d3dd0ff61a51ee0490", "msatoshi" : 1000000000, "amount_msat" : "1000000000msat", "description" : "external_description", "min_final_cltv_expiry" : 10, "routes" : [ [ { "pubkey" : "03a20c0c2ae4508b669a70e5f82f08f69a3da5da3bf0c973673c3fe977db0f61e3", "short_channel_id" : "574012x1255x1", "fee_base_msat" : 1000, "fee_proportional_millionths" : 1, "cltv_expiry_delta" : 144 } ] ], "payment_hash" : "64c0c8f5f8f708b08487ad1376f3d256f92ccb9606987ba395c2b4193efde5a0", "signature" : "304502210099f31dffd18b6db8296637db3e3a1ffbaf7b7db087a2ad1eedb7639f25e78b25022079038370073681aa1e1a7a05de6588e98ead078896d0a37a15b755c212dc3d37" }
lightning-cli decodepay lnbc10m1pwdaj72pp5vnqv3a0c7uytppy845fhdu7j2mujejukq6v8hgu4c26pj0hauksqdpqv4u8getjdeskchmyv4ekxunfwp6xjmmwxqyjw5qcqp2rzjqw3qcrp2u3ggke56wrjlstcg76drmfw680cvjum88sl7ja7mpas7xzxz8sqqfecqqyqqqqlgqqqqqqgqjqn8e3ml733dkms2txxldnuwsllwhhkldss73268hdka3e7f083vjhjqurwqrndqd2rcd85pw7vkywnr4dq7yfd59r0g2mw4wzztwr6dcprxzej6 user@ln-node:/ % lightning-cli decodepay lnbc10m1pwdaj72pp5vnqv3a0c7uytppy845fhdu7j2mujejukq6v8hgu4c26pj0hauksqdpqv4u8getjdeskchmyv4ekxunfwp6xjmmwxqyjw5qcqp2rzjqw3qcrp2u3ggke56wrjlstcg76drmfw680cvjum88sl7ja7mpas7xzxz8sqqfecqqyqqqqlgqqqqqqgqjqn8e3ml733dkms2txxldnuwsllwhhkldss73268hdka3e7f083vjhjqurwqrndqd2rcd85pw7vkywnr4dq7yfd59r0g2mw4wzztwr6dcprxzej6 { "currency" : "bc", "created_at" : 1558105034, "expiry" : 604800, "payee" : "025a14b8ed40583d67aec92da19453e0b2d1fbbf75f96f85d3dd0ff61a51ee0490", "msatoshi" : 1000000000, "amount_msat" : "1000000000msat", "description" : "external_description", "min_final_cltv_expiry" : 10, "routes" : [ [ { "pubkey" : "03a20c0c2ae4508b669a70e5f82f08f69a3da5da3bf0c973673c3fe977db0f61e3", "short_channel_id" : "574012x1255x1", "fee_base_msat" : 1000, "fee_proportional_millionths" : 1, "cltv_expiry_delta" : 144 } ] ], "payment_hash" : "64c0c8f5f8f708b08487ad1376f3d256f92ccb9606987ba395c2b4193efde5a0", "signature" : "304502210099f31dffd18b6db8296637db3e3a1ffbaf7b7db087a2ad1eedb7639f25e78b25022079038370073681aa1e1a7a05de6588e98ead078896d0a37a15b755c212dc3d37" }
But to pay for such an “invoice” we need the pay function from c-lightning:
user@ln-node:/ % lightning-cli pay lnbc2u1pwdana3pp5c0nyfgq974hr8huflt9uutyalj4maaw3q5594xp89jkvw74jme3sdql2pshjgr5dus8q6r0dejjqampd3kx2aqcqzpgwjgn45gy80jwjhgm3tpsxg33j6h6pehdus0mnjerrad943cz3vs83g30lyhlhfjxtqvtl76vttkuhs5jekuxpsqmf98l8265pwmm76gp4e7z6j { "id" : 163, "payment_hash" : "c3e644a005f56e33df89facbce2c9dfcabbef5d105285a98272cacc77ab2de63", "destination" : "03021c5f5f57322740e4ee6936452add19dc7ea7ccf90635f95119ab82a62ae268", "msatoshi" : 200000, "amount_msat" : "200000msat", "msatoshi_sent" : 200003, "amount_sent_msat" : "200003msat", "created_at" : 1558106072, "status" : "complete", "payment_preimage" : "1a9552b9f4e9199e26839353f870b12cc85b7674a2fb134e78aa370032611019", "bolt11" : "lnbc2u1pwdana3pp5c0nyfgq974hr8huflt9uutyalj4maaw3q5594xp89jkvw74jme3sdql2pshjgr5dus8q6r0dejjqampd3kx2aqcqzpgwjgn45gy80jwjhgm3tpsxg33j6h6pehdus0mnjerrad943cz3vs83g30lyhlhfjxtqvtl76vttkuhs5jekuxpsqmf98l8265pwmm76gp4e7z6j" }
lightning-cli pay lnbc2u1pwdana3pp5c0nyfgq974hr8huflt9uutyalj4maaw3q5594xp89jkvw74jme3sdql2pshjgr5dus8q6r0dejjqampd3kx2aqcqzpgwjgn45gy80jwjhgm3tpsxg33j6h6pehdus0mnjerrad943cz3vs83g30lyhlhfjxtqvtl76vttkuhs5jekuxpsqmf98l8265pwmm76gp4e7z6j user@ln-node:/ % lightning-cli pay lnbc2u1pwdana3pp5c0nyfgq974hr8huflt9uutyalj4maaw3q5594xp89jkvw74jme3sdql2pshjgr5dus8q6r0dejjqampd3kx2aqcqzpgwjgn45gy80jwjhgm3tpsxg33j6h6pehdus0mnjerrad943cz3vs83g30lyhlhfjxtqvtl76vttkuhs5jekuxpsqmf98l8265pwmm76gp4e7z6j { "id" : 163, "payment_hash" : "c3e644a005f56e33df89facbce2c9dfcabbef5d105285a98272cacc77ab2de63", "destination" : "03021c5f5f57322740e4ee6936452add19dc7ea7ccf90635f95119ab82a62ae268", "msatoshi" : 200000, "amount_msat" : "200000msat", "msatoshi_sent" : 200003, "amount_sent_msat" : "200003msat", "created_at" : 1558106072, "status" : "complete", "payment_preimage" : "1a9552b9f4e9199e26839353f870b12cc85b7674a2fb134e78aa370032611019", "bolt11" : "lnbc2u1pwdana3pp5c0nyfgq974hr8huflt9uutyalj4maaw3q5594xp89jkvw74jme3sdql2pshjgr5dus8q6r0dejjqampd3kx2aqcqzpgwjgn45gy80jwjhgm3tpsxg33j6h6pehdus0mnjerrad943cz3vs83g30lyhlhfjxtqvtl76vttkuhs5jekuxpsqmf98l8265pwmm76gp4e7z6j" }
All transaction states are processed by the entire network and your own node in particular. There is no need (but there is such an opportunity) to interact with the authors of the application receiving the payment or with some kind of API to obtain information about the status of the payment. There is no need to check the status of the payment in the processing intermediary system. No need to wait for approval of your transaction by a bank or government agency. In the end, there are no charge backs and other external factors that may affect the balance in your account. This is a decision to transfer value via the Internet, without the need for a third party arbiter who decides whether the transaction complies with the rules (sometimes even internal rules about which not all participants are informed in advance) or not. There are no “internal rules” in Bitcoin. The only rules that you need to follow are the rules of the network - open and well-known.
This makes real
direct ,
virtually free and
instant cash transactions between people or programs in a completely autonomous mode.
If you have the opportunity to receive payment data from a third-party application, then you will be able to interact financially with this application. As an example, consider the service lnsms.world, which allows you to send an SMS message to an arbitrary number, paying for it with bitcoins. To integrate this application, we only need to transfer data from the web form directly to the SMS sending script - to emulate user actions on the website:
payload = { 'number': phone_number, 'text': text, 'force_unicode': 0 } send_req = requests.post('https://lnsms.world/invoice', data=payload) if send_req.status_code == 201: plain_invoice = str(send_req.text)
I do not know for what reason, but lnsms responds with 201 HTTP status code and returns the BOLT11 format invoice. Well, this is enough for the bot users to make payment for sending SMS from their balance received in chatikah. The user does not even know what is happening there, he just paid a couple of cents for sending SMS. And I, as a developer, do not go into the details of the work of SMS services. Just forwarded the payment and received the service. You can make a surcharge on top, but you can not do it - this is my free choice. No accounts, agreements and the next API for trivial tasks like sending SMS.
Now let's imagine for the moment that our application is something more useful than a chat bot and makes a payment not to another bot, but to another application or device connected to the Internet. Or is it tens or even hundreds of insignificant payments every second between devices, services, stores or some service providers. This is the power of micro-payments, this is how the Internet of Things should look. There are already projects like
Althea that embody these ideas in life and in the near future we will see great, in my opinion, changes in the area that we are now calling e-commerce.
If you liked the post and you want to try bitcoin in action -
send me a beer, but soft French buns .