Created
January 25, 2017 08:36
-
-
Save weivall/77e4fc418498b2a5276c1aae063520ea to your computer and use it in GitHub Desktop.
Telegram bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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