Hey There! So I've had these 3 things in my head for a couple of months now: : Because I wanted to learn a new Backend language Golang : Because Whatsapp Telegram f@!$ Because printed money Bitcoin: f@!$ 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 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. Hacktoberfest Bot Father Telegram has a Bot of Bots (AKA a Bot-bot). In order to create a new Bot, we will need a . So let's head to Telegram, start a conversation with and ask for a new Token TOKEN @BotFather You can also play a little bit more with . For example, you can use the to define the uses your bot has on the '/' icon: @BotFather /setcommands price - Gets BTC actual price historic - Gets a percentage between Today s price summary - Gets both the price and historic values 's and Yesterday' Bitcoin API After getting the , 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. TOKEN (We all look up for things like this, don't let the impostor syndrome tell you otherwise) I found public API that didn't need a user, token nor anything to consult BTC's price. this With a simple we can get a with the prices. So I decided to use this: CURL JSON 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: model Price { Last PriceBeforeLast Open High Low Vwap Volume Bid Ask } package type struct float32 `json:"last"` float32 `json:"price_before_last"` float32 `json:"open"` float32 `json:"high"` float32 `json:"low"` float32 `json:"vwap"` float32 `json:"volume"` float32 `json:"bid"` float32 `json:"ask"` and then utilizing it on another module which was intended only for the API Call API Call utils ( ) { resp, err := http.Get( ) p := &model.Price{} err != { p, err } err = json.NewDecoder(resp.Body).Decode(p) p, err } package import "encoding/json" "net/http" "github.com/tomassirio/bitcoinTelegram/model" func GetApiCall () (*model.Price, error) "https://bitex.la/api-v1/rest/btc_usd/market/ticker" if nil return return 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. handler ( tb ) { commandMap := ( [ ] { res, _ := commands.GetPrice() b.Send(m.Chat, +res) } commandMap[ ] = { res, g, _ := commands.GetHistoric() b.Send(m.Chat, +res) b.Send(m.Chat, g) } commandMap[ ] = { p, h, _ := commands.GetSummary() b.Send(m.Chat, +p+ +h) } commandMap } package import "github.com/tomassirio/bitcoinTelegram/commands" "gopkg.in/tucnak/telebot.v2" [ ] func LoadHandler (b *tb.Bot) map string func (m *tb.Message) make map string ) ["/ "] = func (m *tb.Message) commandMap price func (m *tb.Message) "BTC's Current price is: U$S " "/historic" func (m *tb.Message) "BTC's Price compared to yesterday is: " "/summary" func (m *tb.Message) "BTC's Current price is: U$S " "\nBTC's Price compared to yesterday is: " return 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 { p, err := utils.GetApiCall() fmt.Sprintf( , p.Last), err } { p, err := utils.GetApiCall() l := p.Last o := p.Open his := ((l - o) / o) * !math.Signbit( (his)) { g := &tb.Animation{File: tb.FromURL( )} + fmt.Sprintf( , ((l-o)/o)* ), g, err } { g := &tb.Animation{File: tb.FromURL( )} + fmt.Sprintf( , *((l-o)/o)* ), g, err } } { p, err := utils.GetApiCall() l := p.Last o := p.Open his := ((l - o) / o) * !math.Signbit( (his)) { fmt.Sprintf( , p.Last), + fmt.Sprintf( , ((l-o)/o)* ), err } { fmt.Sprintf( , p.Last), + fmt.Sprintf( , *((l-o)/o)* ), err } } func GetPrice () ( , error) string return "%.2f" func GetHistoric () ( , *tb.Animation, error) string 100 if float64 "https://i.pinimg.com/originals/e4/38/99/e4389936b099672128c54d25c4560695.gif" return "%" "%.2f" 100 else "http://www.brainlesstales.com/bitcoin-assets/images/fan-versions/2015-01-osEroUI.gif" return "-%" "%.2f" -1 100 func GetSummary () ( , , error) string string 100 if float64 return "%.2f" "%" "%.2f" 100 else return "%.2f" "-%" "%.2f" -1 100 Telegram Config . Of course, there's another module for that. How do you think we could find these things if there was no structure? 'Member that Token we got from @BotFather? Oooh, I 'member Config { Token } { err := godotenv.Load( ) err != { log.Fatalf( ) } &Config{Token: os.Getenv( )} } type struct string * func LoadConfig () Config // load .env file from given path // we keep it empty it will load .env from current directory ".env" if nil "Error loading .env file" return "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: { b, err := tb.NewBot(tb.Settings{ Token: config.LoadConfig().Token, Poller: &tb.LongPoller{Timeout: * time.Second}, }) err != { log.Fatal(err) } k, v := handler.LoadHandler(b) { b.Handle(k, v) log.Println(k + ) } b.Start() } func main () // You can also set custom API URL. // If field is empty it equals to "https://api.telegram.org". 10 if nil return for range "✅ Loaded!" So the program basically gets the 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. TOKEN 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): ~ git https://github.com/tomassirio/BitcoinTelegramBot.git ./BitcoinTelegramBot mv .env.example .env go get github.com/tomassirio/bitcoinTelegram go run main.go cd clone cd Warning: This won't work unless you replace the on the .env file with the Token granted by REPLACE_WITH_TOKEN @BotFather Usage Now head over to Telegram and look for the Bot you created on and use any of the 3 commands that were configured: @BotFather /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 Button: Add to Group 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 footer of the repository in the description below, don't forget to like and subscribe As always, happy coding! balding Also published at https://dev.to/tomassirio/bitcoin-telegram-bot-e1f