Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@IndianGuru
Last active May 22, 2020 11:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save IndianGuru/f1889a9db479293039046ad93b1213ce to your computer and use it in GitHub Desktop.
Save IndianGuru/f1889a9db479293039046ad93b1213ce to your computer and use it in GitHub Desktop.
My First Telegram Bot
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
)
type GetUpdates struct {
Ok bool `json:"ok"`
Result []struct {
UpdateID int `json:"update_id"`
Message struct {
MessageID int `json:"message_id"`
From struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Username string `json:"username"`
} `json:"from"`
Chat struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Username string `json:"username"`
Type string `json:"type"`
} `json:"chat"`
Date int `json:"date"`
Text string `json:"text"`
} `json:"message"`
} `json:"result"`
}
type SendMessage struct {
Ok bool `json:"ok"`
Result struct {
MessageID int `json:"message_id"`
From struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
Username string `json:"username"`
} `json:"from"`
Chat struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Username string `json:"username"`
Type string `json:"type"`
} `json:"chat"`
Date int `json:"date"`
Text string `json:"text"`
} `json:"result"`
}
// Substitute your token here
var token = "Your token here"
func main() {
url := "https://api.telegram.org/bot" + token + "/getUpdates"
nurl := fmt.Sprintf(url)
// Build the request
req, err := http.NewRequest("GET", nurl, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// For control over HTTP client headers,
// redirect policy, and other settings,
// create a Client
// A Client is an HTTP client
client := &http.Client{}
// Send the request via a client
// Do sends an HTTP request and
// returns an HTTP response
resp, err := client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return
}
// Callers should close resp.Body
// when done reading from it
// Defer the closing of the body
defer resp.Body.Close()
// Fill the record with the data from the JSON
var record GetUpdates
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
log.Println(err)
}
chatid := strconv.Itoa(record.Result[0].Message.Chat.ID)
// The Bot now sends a message to chatid
url = "https://api.telegram.org/bot" + token + "/sendMessage?chat_id=" + chatid + "&text='Message from SMTFirstBot for '" + record.Result[0].Message.Chat.Username
nurl = fmt.Sprintf(url)
req, err = http.NewRequest("GET", nurl, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err = client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return
}
defer resp.Body.Close()
var newrecord SendMessage
if err := json.NewDecoder(resp.Body).Decode(&newrecord); err != nil {
log.Println(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment