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. More resources about Bitcoin can be found here and Telegram is simply an instant messenger that allows you to create your custom apps (chat-bots) using their platform. something like this…
 What do 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 another "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 start @atomic_tipbot and make a /deposit. You'll see an invoice, you can select preferred payment method and send money just scanning the QR code or with button press ("open in wallet") on invoice.   Confirm payment in your mobile wallet, and you will see a confirmation screen almost instantly in Telegram. So far we touched two separate parts of the app: telegram bot back-end and payment processing system (actually more like it's front-end). To create this Telegram bot i’ve used Python Telegram Bot. And of course, the bot is open-source. As a payment processing system to generate invoices, check status, send notification and take care of UX is used BTCPayServer which documentation can be found here. If you are looking on how to process payment events directly, you can look at Electrum CLI manual pages for on-chain payments and check out solutions for off-chain (Lightning) payments like Lightning-charge or Sparko. Yes, that’s true. In my app i am using free shared hosting of BTCPayServer instance — BTCPayJungle, 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. In this way, BTCPay architecture makes possible to process payments in trustless manner even on shared environment. It is very simple to configure if you use proven wallets such as Electrum. 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 c-lightning C implementation.  The major difference between on-chain and off-chain (Lightning) payments are in how transactions are stored. All on-chain transactions are broadcasted to the whole network so each running bitcoin node will record this transaction into blockchain, if block containing this transaction will be mined. Hey, BitFury, am i right? ;) That's why we call such transactions on-chain. Such transactions will be stored in blockchain till the end of its existence. It is possible publicly to track, analyse and monitor any such transaction over the network. In case of off-chain (Lightning) transaction things happens in other way. Each lightning node has (may be many) on-chain address, from which one all starts. When funds are received on such address, using cryptography techniques node may open so-called lightning channel to another node, what means that these two nodes made agreement about funds availability between them. Later, all the transactions that are made trough channel (own transactions, or it may be routed transaction from another node) are stored only on those two nodes (and intermediate node if there was such). The only transactions which are stored in networks blockchain are channel opening and closing transactions. This is why Lightning transactions we call off-chain. Mostly they exist only in node database-file. Short story: 1) User sends message and requests deposit to bot on Telegram 2) Telegram triggers bot's 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 is getting data from c-lightning daemon. In a result we get well-handled HTML frame for payment processing 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) According to complete payment detail user balance is changed and user gets notification … if user decides to withdraw funds (tips he received) … 8) Bot's 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. *To be fair, I need to mention that my pay-wrapper paylightning.py is incomplete and may return false-negative in some specific cases. You should use well-tested payment wrapper for c-lightning in case of production-like use. 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 kind of unified and simplified payment network you are able to send same balance 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 ‘number swap in database’ but payment between two separate, non-related ‘databases’. Bot already has few 'LApps' integrated: sat2.io and lnsms.world. You may ask, what exactly means LApp integration? Each Lightning Network node acts as a bitcoin wallet. It has both on-chain and off-chain balance. The on-chain balance usually is used to open channels and store funds from closed channels, in short — nodes maintenance tasks. The off-chain balance is what exactly is used to exchange funds between nodes. The core functions of each node is ability to send or receive funds, so node as a software provides pay and invoice methods. I'll show in short how it happens: The BOLT11 invoice is generated in this case, which is unified format for payment requests in Bitcoin (as well as Litecoin) network. This string includes data about invoice recipient, amount, timestamp, expiration etc. To decode this string c-lightning provides decodepay method: In case we need to pay some invoice, we should use pay method of c-lightning: All the statuses of payments are handled by the whole network and your own node. You don't have a need (but have a possibility to do so) to communicate at all with receiving-side app developers or their API, some card processing provider or any bank to get confirmation about your payment or to release funds from your own account. There are no charge-backs or any other kind of external influence on you accounts balance. All you need is a software which acts by decentralized network rules. This is a solution to exchange a value over internet, without a need of any kind of arbiter in the middle who decides about your payment compatibility with some rules, sometimes even previously unknown to all parties. There are no "internal" rules in bitcoin, all the rules your app needs to complete with are open and known.  This makes possible direct, almost free and instant value exchange over internet between people or even software itself in truly autonomous way. If there is a way to get payment data from other app then you are able to act with it in financial sense. As in our example, lnsms.world is a web-app that allows you to send sms to any number and pay for it with bitcoin. To integrate such option in our app we simply need to pass required data from web-form to lnsms.world: I don't know for what reason, but lnsms responds with 201 HTTP status code and returns BOLT11 invoice. Well, that's enough to pay this invoice and our users will send sms shortly after payment is received. We can charge some fee on top or not, that is our free choice. Now, imagine, that your bot is actually some more useful app and pays not another bot, but some other entity, connected to the Internet. Or, those may be payments between software, machines, 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 like. 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. If you like this post and would try bitcoin payments yourself you can leave me a tip on my tipping page. User