Skip to content

Instantly share code, notes, and snippets.

@karthikeayan
Last active July 14, 2020 08:50
Show Gist options
  • Save karthikeayan/7aa1dd1bc877ddf1d3a02388447942f1 to your computer and use it in GitHub Desktop.
Save karthikeayan/7aa1dd1bc877ddf1d3a02388447942f1 to your computer and use it in GitHub Desktop.
# Replace <your_telegram_api_token> with your Telegram API Token
# Replace <telegram_channel_id> with your Telegram Channel ID where you have to send the alert
package function
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
type Response struct {
Features []AttributesObject `json:"features"`
}
type AttributesObject struct {
Attributes Attributes `json:"attributes"`
}
type Attributes struct {
Positive int `json:"Positive_T"`
Discharge int `json:"Discharge_T"`
Active int `json:"Active_T"`
Death int `json:"Death_T"`
District string `json:"District_Name"`
}
func Common(url string, disctrict string) {
// Get count from government api
response, err := http.Get(url)
if err != nil {
fmt.Printf("The HTTP request failed with error %s\n", err)
}
data, _ := ioutil.ReadAll(response.Body)
// Parse response and get the count
var responseObject Response
json.Unmarshal(data, &responseObject)
message := fmt.Sprintf("Positive: %d\nDischarged: %d\nActive: %d\nDeath: %d\nDistrict: %s",
responseObject.Features[0].Attributes.Positive,
responseObject.Features[0].Attributes.Discharge,
responseObject.Features[0].Attributes.Active,
responseObject.Features[0].Attributes.Death,
responseObject.Features[0].Attributes.District,
)
// Send in telegram
bot, err := tgbotapi.NewBotAPI("<your_telegram_api_token>")
if err != nil {
log.Panic(err)
}
bot.Debug = true
msg := tgbotapi.NewMessage(<telegram_channel_id>, message)
bot.Send(msg)
}
func CoronaTelegram(w http.ResponseWriter, r *http.Request) {
Common("https://services9.arcgis.com/HwXIp55hAoiv6DE9/arcgis/rest/services/TN_District_Points_COVID19_Details/FeatureServer/0/query?f=json&where=FID%3D15&returnGeometry=true&spatialRel=esriSpatialRelIntersects&objectIds=15&outFields=*&outSR=102100&cacheHint=true", "Madurai")
Common("https://services9.arcgis.com/HwXIp55hAoiv6DE9/arcgis/rest/services/TN_District_Points_COVID19_Details/FeatureServer/0/query?f=json&where=FID%3D4&returnGeometry=true&spatialRel=esriSpatialRelIntersects&objectIds=4&outFields=*&outSR=102100&cacheHint=true", "Chennai")
Common("https://services9.arcgis.com/HwXIp55hAoiv6DE9/arcgis/rest/services/TN_District_Points_COVID19_Details/FeatureServer/0/query?f=json&where=FID%3D27&returnGeometry=true&spatialRel=esriSpatialRelIntersects&objectIds=27&outFields=*&outSR=102100&cacheHint=true", "Theni")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment