@baysx

This week I spent coding my very first public pet-app based on Telegram chat bot which acts as a Bitcoin wallet and allows to send and receive tips between Telegram users and other so-called “Lightning Apps”. I assume that you are familiar with Bitcoin & Telegram in general, i’ll try to post short and without deep jump into details.

What are the key points of such app?

*) Allows to rate other users ideas and answers with real value instead of ‘virtual likes’. This brings online conversation to completely new level

*) Real example of working micro-payment app which can act with other entities over internet using open protocol

*) All the modules are open-source projects and can be easy re-used and adjusted for your own project. App does not relay on third-party commercial services. Even it falls under e-commerce field, which is currently almost closed, the app is based on open solutions.

What are the use-cases?

something like this..

or like that..

What the numbers mean? These are bitcoin units — satoshis. As for mid-2019 1000 satoshis has value of $0.06. You can spend it online or exchange to your local currency using many available services. So this is not just a ‘number in a database’ or a ‘coupons you can spend in our partner store’, but a real internet money.

You may ask what to do, if you don’t have any bitcoin to use in the app. You simply can receive a tip from existing app user or buy small amount of bitcoin on various exchanges or bitcoin wallets for mobile phone or pc:

When you got bitcoin, you can deposit it to https://t.me/atomic_tipbot

Start the @atomic_tipbot and make a /deposit

Then you we’ll see an invoice:

You can select preferred payment method and send money just scanning the QR code or with button press on invoice.

Confirm pyment in your mobile wallet:

And you will see a confirmation screen almost instantly in Telegram

So far we touched two separate modules of the app: telegram bot back-end and payment processing system.

To create a Telegram bot i’ve used http://python-telegram-bot.readthedocs.io/

Bots source code: https://github.com/reablaz/atomic_tipbot

As a payment processing system to generate invoices, check status, send notification and take care of UX is used BTCPayServer:

https://www.youtube.com/watch?v=nr0UNbz3AoQ

https://docs.btcpayserver.org/

You said ‘no 3rd party’ and ‘no cloud services’! yes, that’s true. In my app i am using free shared hosting of BTCPayServer instance — https://btcpayjungle.com, but it is possible to spin up even on your own dedicated server. Just remember that you need then about ~500GB of space to store Bitcoin and Litecoin blockchain. BTCPayServer does not hold any private keys or sensitive information that can allow hackers to control funds on you wallet. For on-chain (standart) payments you need to provide xpub key from you wallet to BTCPayServer so it will be able to generate public addresses. It is very simple if you use proven wallets such as http://electrum-ltc.org/ or https://electrum.org/

What is Lightning Network?

It is something like an add-on to Bitcoin which adds additional abstraction layer and allows to make instant and almost free bitcoin transactions. All this stuff is possible thanks to cryptographic multi-signature magic, which is often called smart-contract. There are multiple implementations of LN protocol. I am using https://github.com/ElementsProject/lightning

But it is unsafe to allow access from remote BTCPayServer to ‘wallet’-software CLI to prevent fund stealing. Thats why developers created REST API wrappers only for issuing invoices and checking invoice status — https://github.com/ElementsProject/lightning-charge

The whole thing is visualized on diagram. Short story:

1) User sends message and requests deposit to bot on Telegram

2) Telegram triggers python app

3) python app sends request to BTCPayServer

4) BTCPayServer generates incoming addresses for BTC and LTC, as well contacting lightning-charge, which one controls c-lightning. In a result we get well-handled HTML frame

5) User pays invoice and will see confirmation

6) BTCPayServer sends invoice payment notification (IPN) to specified callback url, in our case it will go to callbacks.py (another python app to listen for callbacks)

7) Bots python app receives notification to top-up user balance for paid amount

… if user decides to withdraw funds (tips he received) …

8) python app listens to text or image of QR, when it gets invoice data (long string of characters) then it sends request to pay that invoice to another python app paylightning.py, which initializes pay-outs using c-lightning RPC

Users are able to send tips to each other on group chats and send/receive value without any limits. But that’s not all. Thanks to Lightning Network protocol, which is in some unified and simplified payment network you are able to send same coins to completely different apps. There is another bitcoin tip-bot on telegram — @lntxbot. User can request a deposit to this bot

Simply forward data from @lntxbot to @atomic_tipbot and voul’a!

This is not anymore ‘digit swap in database’ but payment between two separate ‘databases’. Bot already has few 'LApps' integrated: sat2.io and lnsms.world

Now, imagine, that your bot pays not another bot, but some device, connected to internet. Or those are payments are between devices, bots, stores, service providers on regular basis repeating each few seconds? Do you feel the power of micro-payments? That is how the Internet of Things will look. Already now there are such projects as https://althea.org/ and in near future we will face great changes in field what we call e-commerce.

Let’s build new internet together, cheers!

If you like post, feel free to https://tip.ysx.in/

cross-link to telegra.ph: https://telegra.ph/How-to-make-possible-micro-payments-in-your-app-05-11