Hackernoon logoHow to Build a Bitcoin Telegram Bot by@tomassirio

How to Build a Bitcoin Telegram Bot

Tomas Sirio Hacker Noon profile picture

@tomassirioTomas Sirio

I'm a Barbie Dev, In a Barbie World.Wide.Web Backend Developer at Ualá. Master of memes.

Hey There!

So I've had these 3 things in my head for a couple of months now:

I got an idea that you would not be able to guess...

That's right, merging the 3 into a single project.

Bitcoin Telegram Bot

Since I created a couple of bots for the last Hacktoberfest I decided to create a simple Telegram bot that could retrieve Bitcoin's Price. I'm doing this to convince my friends that Telegram is far superior to Whatsapp.

Bot Father

Telegram has a Bot of Bots (AKA a Bot-bot). In order to create a new Bot, we will need a TOKEN. So let's head to Telegram, start a conversation with @BotFather and ask for a new Token

You can also play a little bit more with @BotFather. For example, you can use the /setcommands to define the uses your bot has on the '/' icon:

price - Gets BTC actual price
historic - Gets a percentage between Today's and Yesterday's price
summary - Gets both the price and historic values

Bitcoin API

After getting the TOKEN, I needed Bitcoin's price. And as far as I know, I wasn't going to hardcode it into a Bot or hot-swap its value manually. So I looked for an API that could do that for me.

(We all look up for things like this, don't let the impostor syndrome tell you otherwise)

I found this public API that didn't need a user, token nor anything to consult BTC's price.

With a simple CURL we can get a JSON with the prices. So I decided to use this:

A little bit of code

(You can skip this section if you don't like boring code. But there are memes)

If you read my previous posts, I've been researching and testing how GO modules worked. I don't like convoluted files where I can't find where things are, so my folder structure looks like this:

Model

I began by writing a model for Bitex's response:

package model

type Price struct {
	Last            float32 `json:"last"`
	PriceBeforeLast float32 `json:"price_before_last"`
	Open            float32 `json:"open"`
	High            float32 `json:"high"`
	Low             float32 `json:"low"`
	Vwap            float32 `json:"vwap"`
	Volume          float32 `json:"volume"`
	Bid             float32 `json:"bid"`
	Ask             float32 `json:"ask"`
}

and then utilizing it on another module which was intended only for the API Call

API Call

package utils

import (
	"encoding/json"
	"net/http"

	"github.com/tomassirio/bitcoinTelegram/model"
)

func GetApiCall() (*model.Price, error) {
	resp, err := http.Get("https://bitex.la/api-v1/rest/btc_usd/market/ticker")
	p := &model.Price{}

	if err != nil {
		return p, err
	}

	err = json.NewDecoder(resp.Body).Decode(p)
	return p, err
}

Lovely GO handles REST requests like a boss.

Handler

The handler is a little bit sloppily written. I've been using JavaScript (Which I'm definitely not keen on) to create Discord Bots. So I tried to emulate my previous handlers in GO... so we have this now.

package handler

import (
	"github.com/tomassirio/bitcoinTelegram/commands"
	tb "gopkg.in/tucnak/telebot.v2"
)

func LoadHandler(b *tb.Bot) map[string]func(m *tb.Message) {
	commandMap := make(map[string]func(m *tb.Message))

	commandMap["/price"] = func(m *tb.Message) {
		res, _ := commands.GetPrice()
		b.Send(m.Chat, "BTC's Current price is: U$S "+res)
	}

	commandMap["/historic"] = func(m *tb.Message) {
		res, g, _ := commands.GetHistoric()
		b.Send(m.Chat, "BTC's Price compared to yesterday is: "+res)
		b.Send(m.Chat, g)
	}

	commandMap["/summary"] = func(m *tb.Message) {
		p, h, _ := commands.GetSummary()
		b.Send(m.Chat, "BTC's Current price is: U$S "+p+"\nBTC's Price compared to yesterday is: "+h)
	}

	return commandMap
}

What's most remarkable about this handler is that it's basically a Map from String to a function. This will make sense once we get to the main function.

Commands

I only created 3 commands for this bot. It wasn't a deep project, it was mostly for the lulz, so bear with the simplicity of them for now

func GetPrice() (string, error) {
	p, err := utils.GetApiCall()
	return fmt.Sprintf("%.2f", p.Last), err
}

func GetHistoric() (string, *tb.Animation, error) {
	p, err := utils.GetApiCall()
	l := p.Last
	o := p.Open
	his := ((l - o) / o) * 100
	if !math.Signbit(float64(his)) {
		g := &tb.Animation{File: tb.FromURL("https://i.pinimg.com/originals/e4/38/99/e4389936b099672128c54d25c4560695.gif")}
		return "%" + fmt.Sprintf("%.2f", ((l-o)/o)*100), g, err
	} else {
		g := &tb.Animation{File: tb.FromURL("http://www.brainlesstales.com/bitcoin-assets/images/fan-versions/2015-01-osEroUI.gif")}
		return "-%" + fmt.Sprintf("%.2f", -1*((l-o)/o)*100), g, err
	}
}

func GetSummary() (string, string, error) {
	p, err := utils.GetApiCall()
	l := p.Last
	o := p.Open
	his := ((l - o) / o) * 100
	if !math.Signbit(float64(his)) {
		return fmt.Sprintf("%.2f", p.Last), "%" + fmt.Sprintf("%.2f", ((l-o)/o)*100), err
	} else {
		return fmt.Sprintf("%.2f", p.Last), "-%" + fmt.Sprintf("%.2f", -1*((l-o)/o)*100), err
	}
}

Telegram Config

'Member that Token we got from @BotFather? Oooh, I 'member. Of course, there's another module for that. How do you think we could find these things if there was no structure?

type Config struct {
	Token string
}

func LoadConfig() *Config {
	// load .env file from given path
	// we keep it empty it will load .env from current directory
	err := godotenv.Load(".env")

	if err != nil {
		log.Fatalf("Error loading .env file")
	}

	return &Config{Token: os.Getenv("TOKEN")}
}

I'm not going to copy the contents of the .env file, but I will show you the .env.example

TOKEN=REPLACE_WITH_TOKEN

I really hope you weren't expecting anything else.

The main file

Our Bot is almost built, now we have to tell Go: Dude, we need this up and running:

func main() {

	b, err := tb.NewBot(tb.Settings{
		// You can also set custom API URL.
		// If field is empty it equals to "https://api.telegram.org".
		Token:  config.LoadConfig().Token,
		Poller: &tb.LongPoller{Timeout: 10 * time.Second},
	})

	if err != nil {
		log.Fatal(err)
		return
	}

	for k, v := range handler.LoadHandler(b) {
		b.Handle(k, v)
		log.Println(k + "✅ Loaded!")
	}

	b.Start()

}

So the program basically gets the TOKEN from the Config module, it checks that there were no errors, and (here comes my favorite part) we cycle through the command map on the Handler module in order to load every single command onto the Bot.

Let's look at it again since it was so satisfying:

Hosting it

I used my Raspberry 4 to host this bot (as well as the others) but you can use Heroku, AWS, Gcloud, or just an old computer that you are not using right now

To host it you will need:

  • A computer, service or server where to host the bot.
  • Git
  • Golang v1.13
  • A device with Telegram (to use it)

Open a Terminal and copy these commands (Linux & Mac devices):

cd ~
git clone https://github.com/tomassirio/BitcoinTelegramBot.git
cd ./BitcoinTelegramBot
mv .env.example .env
go get github.com/tomassirio/bitcoinTelegram
go run main.go

Warning:

This won't work unless you replace the REPLACE_WITH_TOKEN on the .env file with the Token granted by @BotFather

Usage

Now head over to Telegram and look for the Bot you created on @BotFather and use any of the 3 commands that were configured:

  • /price : Get's bitcoin's Last price
  • /historic : Gets a percentage between Today's and Yesterday's price
  • /summary : Gets both the price and historic values

Also, you can add it to your favorite group (or any group whatsoever) by tapping on the chat's options and then the Add to Group Button:

Final Words

I really hoped you enjoyed this post as much as I did writing it. I don't consider myself a good developer, but that's exactly why I'm doing these posts. You don't need to be an expert in order to share your experiences with other people. Also, every opportunity to create more memes should be approached as fast and as creatively as possible.

If you want to check out the repository, you can do so by going to the following link.

  • You can Fork it and do your own version of it.
  • You can tweak it and create a pull request to merge it to the original version.
  • You can do whatever you want with it because it's open-source.
  • If you want to leave a Star on it, I'll be really grateful since it will help me in my professional career.
  • If you want to buy me a beer, the link is in the description below, don't forget to like and subscribe in the footer of the repository

As always, happy balding coding!

Also published at https://dev.to/tomassirio/bitcoin-telegram-bot-e1f

Tomas Sirio Hacker Noon profile picture
by Tomas Sirio @tomassirio. I'm a Barbie Dev, In a Barbie World.Wide.Web Backend Developer at Ualá. Master of memes. Read my stories

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.