Public Access
добавил интеграцию с сайтом
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -32,42 +34,109 @@ func main() {
|
||||
forward := func(text string, from string, sender string) {
|
||||
log.Printf("[Bridge] Пересылка сообщения от %s (%s): %s", from, sender, text)
|
||||
|
||||
switch from {
|
||||
case "telegram":
|
||||
if discordChannelID != "" {
|
||||
_ = bridge.SendToDiscord(discordChannelID, "[TG] "+sender+": "+text)
|
||||
}
|
||||
if vkPeerID != "" {
|
||||
if id, err := strconv.Atoi(vkPeerID); err == nil {
|
||||
_ = bridge.SendToVK(id, "[TG] "+sender+": "+text)
|
||||
}
|
||||
}
|
||||
case "discord":
|
||||
if telegramChatID != "" {
|
||||
if id, err := strconv.ParseInt(telegramChatID, 10, 64); err == nil {
|
||||
_ = bridge.SendToTelegram(id, "[DS] "+sender+": "+text)
|
||||
}
|
||||
}
|
||||
if vkPeerID != "" {
|
||||
if id, err := strconv.Atoi(vkPeerID); err == nil {
|
||||
_ = bridge.SendToVK(id, "[DS] "+sender+": "+text)
|
||||
}
|
||||
}
|
||||
case "vk":
|
||||
if telegramChatID != "" {
|
||||
if id, err := strconv.ParseInt(telegramChatID, 10, 64); err == nil {
|
||||
_ = bridge.SendToTelegram(id, "[VK] "+sender+": "+text)
|
||||
}
|
||||
}
|
||||
if discordChannelID != "" {
|
||||
_ = bridge.SendToDiscord(discordChannelID, "[VK] "+sender+": "+text)
|
||||
}
|
||||
}
|
||||
switch from {
|
||||
case "telegram":
|
||||
if discordChannelID != "" {
|
||||
_ = bridge.SendToDiscord(discordChannelID, "[TG] "+sender+": "+text)
|
||||
}
|
||||
if vkPeerID != "" {
|
||||
if id, err := strconv.Atoi(vkPeerID); err == nil {
|
||||
_ = bridge.SendToVK(id, "[TG] "+sender+": "+text)
|
||||
}
|
||||
}
|
||||
_ = bridge.SendToSite("[TG] " + sender + ": " + text)
|
||||
case "discord":
|
||||
if telegramChatID != "" {
|
||||
if id, err := strconv.ParseInt(telegramChatID, 10, 64); err == nil {
|
||||
_ = bridge.SendToTelegram(id, "[DS] "+sender+": "+text)
|
||||
}
|
||||
}
|
||||
if vkPeerID != "" {
|
||||
if id, err := strconv.Atoi(vkPeerID); err == nil {
|
||||
_ = bridge.SendToVK(id, "[DS] "+sender+": "+text)
|
||||
}
|
||||
}
|
||||
_ = bridge.SendToSite("[DS] " + sender + ": " + text)
|
||||
case "vk":
|
||||
if telegramChatID != "" {
|
||||
if id, err := strconv.ParseInt(telegramChatID, 10, 64); err == nil {
|
||||
_ = bridge.SendToTelegram(id, "[VK] "+sender+": "+text)
|
||||
}
|
||||
}
|
||||
if discordChannelID != "" {
|
||||
_ = bridge.SendToDiscord(discordChannelID, "[VK] "+sender+": "+text)
|
||||
}
|
||||
_ = bridge.SendToSite("[VK] " + sender + ": " + text)
|
||||
case "site":
|
||||
if telegramChatID != "" {
|
||||
if id, err := strconv.ParseInt(telegramChatID, 10, 64); err == nil {
|
||||
_ = bridge.SendToTelegram(id, "[SITE] "+sender+": "+text)
|
||||
}
|
||||
}
|
||||
if discordChannelID != "" {
|
||||
_ = bridge.SendToDiscord(discordChannelID, "[SITE] "+sender+": "+text)
|
||||
}
|
||||
if vkPeerID != "" {
|
||||
if id, err := strconv.Atoi(vkPeerID); err == nil {
|
||||
_ = bridge.SendToVK(id, "[SITE] "+sender+": "+text)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bridge.StartTelegramListener(func(text, sender string) { forward(text, "telegram", sender) })
|
||||
bridge.StartDiscordListener(func(text, sender string) { forward(text, "discord", sender) })
|
||||
bridge.StartVKListener(func(text, sender string) { forward(text, "vk", sender) })
|
||||
bridge.StartTelegramListener(func(text, sender string) { forward(text, "telegram", sender) })
|
||||
bridge.StartDiscordListener(func(text, sender string) { forward(text, "discord", sender) })
|
||||
bridge.StartVKListener(func(text, sender string) { forward(text, "vk", sender) })
|
||||
|
||||
select {} // Блокируем main, чтобы слушатели работали
|
||||
// webhook endpoint
|
||||
http.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
apiKey := os.Getenv("WEBHOOK_API_KEY")
|
||||
if apiKey == "" {
|
||||
http.Error(w, "Webhook API key not set", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
key := r.URL.Query().Get("key")
|
||||
if key == "" {
|
||||
// также можно принимать ключ в теле запроса
|
||||
var req struct {
|
||||
Key string `json:"key"`
|
||||
Text string `json:"text"`
|
||||
Sender string `json:"sender"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "Bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if req.Key != apiKey {
|
||||
http.Error(w, "Forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
forward(req.Text, "site", req.Sender)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("ok"))
|
||||
return
|
||||
}
|
||||
if key != apiKey {
|
||||
http.Error(w, "Forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Text string `json:"text"`
|
||||
Sender string `json:"sender"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "Bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
forward(req.Text, "site", req.Sender)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("ok"))
|
||||
})
|
||||
|
||||
log.Println("Webhook listening on :8080 (/webhook)")
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user