-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
62 lines (49 loc) · 1.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
m "./models"
)
const (
// TOKEN telegram
TOKEN = "YOUR-TOKEN"
// URL telegram
URL = "https://api.telegram.org/bot"
// PORT local
PORT = "3000"
)
func update(w http.ResponseWriter, r *http.Request) {
message := &m.ReceiveMessage{}
chatID := 0
msgText := ""
err := json.NewDecoder(r.Body).Decode(&message)
if err != nil {
fmt.Println(err)
}
// if private or group
if message.Message.Chat.ID != 0 {
fmt.Println(message.Message.Chat.ID, message.Message.Text)
chatID = message.Message.Chat.ID
msgText = message.Message.Text
} else {
// if channel
fmt.Println(message.ChannelPost.Chat.ID, message.ChannelPost.Text)
chatID = message.ChannelPost.Chat.ID
msgText = message.ChannelPost.Text
}
respMsg := fmt.Sprintf("%s%s/sendMessage?chat_id=%d&text=Received: %s", URL, TOKEN, chatID, msgText)
// send echo resp
_, err = http.Get(respMsg)
if err != nil {
fmt.Println(err)
}
}
func main() {
http.HandleFunc("/api/v1/update", update)
fmt.Println("Listenning on port", PORT, ".")
if err := http.ListenAndServe(":"+PORT, nil); err != nil {
log.Fatal(err)
}
}