Receive an SMS from Anyone for Bitcoin

Posted by John Granata

Receive an SMS from Anyone for Bitcoin

Overview

In this tutorial we'll show you how to set up a server that will allow anyone to send you an SMS for a small amount of bitcoin - without revealing your phone number. The example can be extended to any other form of out-of-network social messaging, such as an email from someone you haven't met or a Twitter DM from someone you haven't followed. It's a way to monetize your inbox and widen the circle of people who can contact you.

Prerequisites

Install 21

You will need the following items:

If you've got all the prerequisites, you are ready to go. Let's get started!

Step 1: Sign up for a Twilio API key

Go to twilio.com and create an account. Take note of your account SID, auth token, and Twilio phone number. Then install the Twilio Python3 package:

sudo pip3 install twilio

We'll use this to send the SMS.

Step 2: Set up the SMS server

Open up a new terminal window. If you use a 21 Bitcoin Computer, ssh into it:

ssh twenty@IP_ADDRESS

Create a folder to house your project:

mkdir bitcoin-sms-server && cd bitcoin-sms-server

Use a text editor to create a file called sms-server.py in the bitcoin-sms-server directory, and fill it with the following code:

#!/usr/bin/env python3
import os

from twilio.rest import TwilioRestClient
from flask import Flask, request

from two1.wallet import Wallet
from two1.bitserv.flask import Payment

app = Flask(__name__)
wallet = Wallet()
payment = Payment(app, wallet)

# create the twilio rest client
client = TwilioRestClient(
    os.environ.get('TWILIO_ACCOUNT_SID'),
    os.environ.get('TWILIO_AUTH_TOKEN')
)


@app.route('/send-sms')
@payment.required(3000)
def send_sms():
    """Send an sms for bitcoin"""
    text = request.args.get('text')
    client.messages.create(
        to=os.environ.get('MY_PHONE_NUMBER'),
        from_=os.environ.get('TWILIO_PHONE_NUMBER'),
        body=text
    )
    return "Message sent."

# set up and run the server
if __name__ == '__main__':
    app.run(host='::')

Save the file and close it. Set environment variables in your terminal as follows:

export TWILIO_ACCOUNT_SID=<Your Twilio Account SID>
export TWILIO_AUTH_TOKEN=<Your Twilio Auth Token>
export TWILIO_PHONE_NUMBER=<Your Twilio Phone Number>
export MY_PHONE_NUMBER=<Your Phone Number>

Start the micropayments server:

python3 sms-server.py

Your bitcoin-powered SMS endpoint is up and running! Let's get a client user set up to test the endpoint.

Step 3: Set up the client who will pay to SMS you

Leave your session open, but open a new terminal. If you have a 21 Bitcoin Computer, ssh into it:

## Only necessary if you use a 21 Bitcoin Computer
ssh twenty@IP_ADDRESS

Create a file called sms-client.py with the following code:

#!/usr/bin/env python3
import urllib.parse

from two1.wallet import Wallet
from two1.bitrequests import BitTransferRequests

wallet = Wallet()
requests = BitTransferRequests(wallet)


# request the bitcoin-enabled endpoint that you're hosting
def testendpoint(sms='I just paid you bitcoin to send you this message!'):
    # In a real application you would escape this message to prevent injections
    message = urllib.parse.quote_plus(sms)
    response = requests.get(url='http://[::1]:5000/send-sms?text='+message)
    print(response.text)

if __name__ == '__main__':
    import sys
    testendpoint(sys.argv[1])

You can run this client script from the command line to test your SMS endpoint:

python3 sms-client.py 'I am SMSing you for bitcoin!'

You should receive a text message at your phone number. Congratulations, you just set up a way for someone to send you a text message for bitcoin!

Next Steps

You've learned how to build a very simple app in which someone outside your social network can pay you bitcoin to contact you. This is actually something that people have wanted on the internet for a very long time, since the development of Hashcash and before. Here are some ideas for what to build next:

  • Extend the example to escape the input text message and otherwise be robust against bad input strings

  • Add support for paying bitcoin to contact you via email, Facebook, Twitter, LinkedIn , and other forms of social messaging.

  • Create an inbox viewer which rank orders your received messages not by the time they were received, but by the amount of bitcoin sent with them (a new kind of "priority inbox")

  • Make this example high performance by replacing the simple Flask server with a proper deployment on the underlying hardware. See this and this for more information.

If you build anything like this and want to earn some bitcoin for your efforts, write it up and submit it as a bitcoin tutorial. If we decide to publish it on our site, you'll win $200 in BTC!

You can also come to our Slack channel at slack.21.co to find other 21 users to post your endpoints and give feedback. We look forward to seeing you there!


How to send your Bitcoin to the Blockchain

Just as a reminder, you can send bitcoin mined or earned in your 21.co balance to the blockchain at any time by running 21 flush . A transaction will be created within 10 minutes, and you can view the transaction id with 21 log. Once the transaction has been confirmed, you can check the balance in your bitcoin wallet from the command line with wallet balance, and you can send bitcoin from your wallet to another address with wallet sendto $BITCOIN_ADDRESS --satoshis $SATOSHI_AMOUNT --use-unconfirmed. The --satoshis flag allows you to specify the amount in satoshis; without it the sendto amount is in BTC, but this behavior is deprecated and will be removed soon. The --use-unconfirmed flag ensures that you can send even if you have unconfirmed transactions in your wallet.


Ready to sell your endpoint? Go to slack.21.co

Ready to try out your bitcoin-payable server in the wild? Or simply want to browse and purchase from other bitcoin-enabled servers? Head over to the 21 Developer Community at slack.21.co to join the bitcoin machine-payable marketplace hosted on the 21 peer-to-peer network.