Make a Compliment Telegram Bot in Golang

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
Search for a command to run...

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
No comments yet. Be the first to comment.
What is gRPC? gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authenti...
Let's assume we have a sample Golang application (in a Go project folder). Contents of main.go package main import "fmt" func main() { fmt.Println("hello world") } In the same folder, run the following commands to initialize go modules # initial...

When you create a new Kubernetes Cluster in EKS - you still need to do several things until you can start using it. This simple set of steps aims to help people create an EKS cluster in simple steps and configure the most essential services like Log ...

To implement a stack in golang we can make use of the slice datatype which golang provides which allows us to modify the size on the fly. If we were doing it in C, we would need to define a max size for the array. Lets start with a simple structure ...

The purpose of this guide is to make a simple telegram bot, which replies with a compliment for each and every message that it receives.
/newbotcomplimentrbotbot link and bot token to access the bot.Get the basic example of telegram echo bot from here
In the example, change the MyAwesomeBotToken to the actual bot token which we got in the above step.
https://complimentr.com/api (check this link to see the API response).
Now, we write a function in our main.go called getCompliment().
func getCompliment() (string, error) {
// make a get call to the api endpoint
resp, err := http.Get("https://complimentr.com/api")
if err != nil {
return "", err
}
defer resp.Body.Close()
// read the body in bytes
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
m := make(map[string]string)
// convert json data into a map
err = json.Unmarshal(body, &m)
if err != nil {
return "", err
}
// return the compliment string
return m["compliment"], nil
}
c, err := getCompliment()
if err!=nil{
fmt.Println("Error while getting compliment")
continue
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, c)
package main
import (
"encoding/json"
"io/ioutil"
"fmt"
"os"
"log"
"net/http"
"github.com/go-telegram-bot-api/telegram-bot-api"
)
func getCompliment() (string, error) {
resp, err := http.Get("https://complimentr.com/api")
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
m := make(map[string]string)
err = json.Unmarshal(body, &m)
if err != nil {
return "", err
}
return m["compliment"], nil
}
func main() {
token := os.Getenv("TELEGRAM_TOKEN")
bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil { // ignore any non-Message Updates
continue
}
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
c, err := getCompliment()
if err!=nil{
fmt.Println("Error while getting compliment")
continue
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, c)
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
}
}