Search for:
Crypto Development

LocalBitcoins AutoResponder: Free Python-based autoresponder for LBTC

Pinterest LinkedIn Tumblr

Warning: the code and concepts covered in this article are for proof of concept only. I am not responsible should any unsavoury outcomes occur.

This gem I’ve been waiting to share for a long time. But given it’s a new year, and I’m giving True Miller a good hard shove, its time to release some value.

I’m going to show you guys how to build an automated LocalBitcoins autoresponder. A bot, software, that will automatically respond to your trade contacts.

This an incredibly good way to keep your response time as low as possible, and is perfect for those of you that’re getting inundated with trades in the current climate.

If you’re not already on LocalBitcoins and/or you’ve got no idea what I’m talking about:

Without further ado, lets get into the magic.

Requirements

Fulfil these requirements in order.

It’s important that you get an account first. Complete verification sooner rather than later if you haven’t already joined.

image 11
Verification is simple for Tier 0; input your email, name and country of residence.

Then, install Python, followed by PIP. Clone the LBCAPI repo and put it in a folder that you’ll use to store your scripts later.

There’s quite a lot of technical coverage in this tutorial. If you’re a developer and you want to deviate from the guide, you can check out Localbitcoins’ API docs. Otherwise, this is probably not for you.

If you have questions, leave a comment and/or join the Discord.


Building a LocalBitcoins Autoresponder

In this section, we’ll actually build the bot. I’m assuming you’ve already got all the relevant requirements.

Note: the basic connection is vital; all other sections will extend the basic connection, and can be pasted below the basic connection script.

Connect to LocalBitcoins API with Python

1. Generate LocalBitcoins HMAC (API) keys

Provided you’ve signed up, you’ll be able to generate an HMAC key from the Apps Dashboard.

image 12
You’ll need HMAC keys if you want to use this software.

This key will allow your application to access your LocalBitcoins account directly.

2. Create a new Python script

Create a new folder, drop the LBCAPI library into it, and create a new Python script. For example, bot.py.

image 15
See how the lbcapi folder is in the same folder as the scripts I’ve created.

You can call the scripts anything. But for simplicities sake, you could call yours bot.py.

'''
bot.py
My LocalBitcoins Bot
'''
import json, time, datetime

# Import LCBAPI; this will give us some useful functionality to connect to the Localbitcoins API
from lbcapi import api

# Store your localbitcoins credentials here; HMAC and username
creds = {
    username: 'truemiller6969',
    hmac_key: 'abcdefg',
    hmac_secret: '123456'
}

# Connection; what we will use to connect to the API
connection = api.hmac(creds['hmac_key'], creds['hmac_secret'])

Autoresponder

Please note, this code is only proof of concept and comes with no warranties, liability, nor safety precautions. Only run this if you know what you’re doing!

As I mentioned earlier, the idea behind running an autoresponder is so you keep your response time to a minimum, and you don’t miss any trades. Flipping Bitcoin on LocalBitcoins can get a bit overwhelming if you’re getting a lot of orders.

This software will automate the first message to your customer; welcome the customer, send the relevant details, and alternate dependant on whether or not the trade is to BUY or SELL bitcoin.

Different messages should be sent to buyers and sellers. For example, sellers need your account details, buyers need to send their details.

If you run the scripts it’s at your own risk. If you edit it, its at your own risk. The following is simply proof of concept.

'''
bot.py
My LocalBitcoins Bot
'''
import json, time, datetime

# Import LCBAPI; this will give us some useful functionality to connect to the Localbitcoins API
from lbcapi import api

# Store your localbitcoins credentials here; HMAC and username
creds = {
    username: 'truemiller6969',
    hmac_key: 'abcdefg',
    hmac_secret: '123456'
}

# Connection; what we will use to connect to the API
connection = api.hmac(creds['hmac_key'], creds['hmac_secret'])

'''
FUNCTIONS 
'''
def getDashboard(connection = None)
	dashboard = conn.call('GET', '/api/dashboard/').json()['data']
	return dashboard

def getMessages(connection=None, contact_id="123456"):
	messages = connection.call('GET', '/api/contact_messages/%s/'%(contact_id)).json()
	return messages


'''
MAIN FUNCTION
'''
def main():
	while True: # loop forever, or until cancel (Ctrl+C)

		# Reset variables
		localBitcoinsDashboard = localBitcoinsDashboardContacts = []
		trade_type=contact_id=buyer=payment_completed_at=contact_reference=seller=amount=payment_completed_at=messages=None

		# Print the time (for updates)
		print(datetime.datetime.now())

		# Get all the contacts (open trades)
		localBitcoinsDashboardContacts = getDashboard(connection)['contact_list']

		# If there are any contacts
		if len(localBitcoinsDashboardContacts) > 0:
			# Tell user how many trades there are
			print("╿ " + "You have %d open trades." % (len(getDashboard['contact_list']))) 
			# Loop through all the contacts
			for contact in localBitcoinsDashboardContacts:  
				# store the contact data in easily accessed variables  
				trade_type = contact['data']['advertisement']['trade_type']
				contact_id = contact['data']['contact_id']          
				buyer = contact['data']['buyer']
				payment_completed_at = contact['data']['payment_completed_at']
				contact_reference = contact['data']['reference_code']
				seller = contact['data']['seller']
				amount = contact['data']['amount']
				payment_completed_at = contact['data']['payment_completed_at']
				# get messages from the contact
				messages = getMessages(connection, contact_id)['data']
				# print progress dependant on type of trade
				if trade_type == 'ONLINE_BUY':
					print((("[" + str(datetime.datetime.now()) + "]") + ' %d %s %s %s %s') % (contact_id, seller['name'], trade_type, amount, payment_completed_at))
				else:
					print(("[" + str(datetime.datetime.now()) + "]") + ' %d %s %s %s %s' % (contact_id, buyer['name'], trade_type, amount, payment_completed_at))
			
			## AUTO RESPONDER
			# Send first nmessage 
			your_response_count = 0
			for message in messages['message_list']:
				if message['sender']['username'] == creds['username']:
					your_response_count = your_response_count + 1
				else:
					your_response_count = your_response_count   


			if your_response_count <= 0 and trade_type == 'ONLINE_SELL':
				print('┗━ Sending seller message.')
				print(json.dumps(connection.call('post', '/api/contact_message_post/' + str(contact_id) + '/', params={
					'msg': 'Hi, thanks for agreeing to the terms by opening a trade.+\n\n'
					'My Bitcoin is now in Escrow, please make payment ASAP.'
					' If you have no intentions of continuing with the trade, please cancel the trade so my Bitcoin is released back to me and I can continue trading.\n\n'
					'+== REFERENCE MUST BE EXACT (NOTE, THIS UPDATES EVERY TRADE) ==+\n\n\n\n'
					'Details are as follows\n+=====================+\n'
					'Name: [NAME ON ACCOUNT]\n'
					'Sort Code: 123456\n'
					'Account Number: 123456789\n'
					'Reference: ' + str(contact_reference) + ' \n'
					'+=====================+\nPlease, MARK AS PAID once you have paid.'}).json()))
			elif your_response_count <= 0 and trade_type == 'ONLINE_BUY':
				print('┗━ Sending buyer message')
				print(json.dumps(connection.call('post', '/api/contact_message_post/' + str(contact_id) + '/', params={
					'msg': '.\n Hi there, just a moment and your funds will be on their way!\n'}).json()))
			else:
				print('┗━ Sending no message')
		time.sleep(10) # IMPORTANT, DONT SPAM THE API

Conclusion

Building a LocalBitcoins autoresponder is fairly simple. Use the code examples above, tinker with it, change it, and thank me for it!

If you don’t know what this bot can be used for… check out my LocalBitcoins flipping guide.

I do have more things I can release too. For example a LocalBitcoins auto-pricer, that competitively prices your listings against others‘.

If you’d like to get hold of this, drop a comment and/join the discord!

Until next time,

Josh

Leave a Reply