-
Notifications
You must be signed in to change notification settings - Fork 66
/
handle_message.go
44 lines (41 loc) · 1.32 KB
/
handle_message.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
package telegram
import (
"fmt"
"strconv"
"strings"
"github.com/tucnak/telebot"
)
// handleMessage handles incoming messages to start sending events to subscribers chats
func (sender *Sender) handleMessage(message *telebot.Message) error {
var err error
id := strconv.FormatInt(message.Chat.ID, 10)
title := message.Chat.Title
userTitle := strings.Trim(fmt.Sprintf("%s %s", message.Sender.FirstName, message.Sender.LastName), " ")
username := message.Chat.Username
chatType := message.Chat.Type
switch {
case chatType == "private" && message.Text == "/start":
if username == "" {
sender.bot.Send(message.Chat, "Username is empty. Please add username in Telegram.")
} else {
err = sender.DataBase.SetUsernameID(messenger, "@"+username, id)
if err != nil {
return err
}
sender.bot.Send(message.Chat, fmt.Sprintf("Okay, %s, your id is %s", userTitle, id))
}
case chatType == "supergroup" || chatType == "group":
uid, _ := sender.DataBase.GetIDByUsername(messenger, title)
if uid == "" {
sender.bot.Send(message.Chat, fmt.Sprintf("Hi, all!\nI will send alerts in this group (%s).", title))
}
fmt.Println(chatType, title)
err = sender.DataBase.SetUsernameID(messenger, title, id)
if err != nil {
return err
}
default:
sender.bot.Send(message.Chat, "I don't understand you :(")
}
return err
}