Repository navigation
Expand file tree
/
Copy pathcontext.go
More file actions
167 lines (139 loc) · 3.63 KB
/
Copy pathcontext.go
File metadata and controls
167 lines (139 loc) · 3.63 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package telego
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
)
type Context struct {
Bot *Bot
ChatID int64
Message *Message
CallbackQuery *CallbackQuery
Args []string
}
// ============ Helper Functions ============
// Text mesaj içeriğini döndürür
func (c *Context) Text() string {
if c.Message != nil {
return c.Message.Text
}
return ""
}
// UserName kullanıcı adını döndürür
func (c *Context) UserName() string {
if c.Message != nil {
return c.Message.From.Username
}
if c.CallbackQuery != nil {
return c.CallbackQuery.From.Username
}
return ""
}
// FirstName kullanıcının adını döndürür
func (c *Context) FirstName() string {
if c.Message != nil {
return c.Message.From.FirstName
}
if c.CallbackQuery != nil {
return c.CallbackQuery.From.FirstName
}
return ""
}
// UserID kullanıcı ID'sini döndürür
func (c *Context) UserID() int {
if c.Message != nil {
return c.Message.From.ID
}
if c.CallbackQuery != nil {
return c.CallbackQuery.From.ID
}
return 0
}
// MessageID mesaj ID'sini döndürür
func (c *Context) MessageID() int {
if c.Message != nil {
return c.Message.MessageID
}
if c.CallbackQuery != nil && c.CallbackQuery.Message != nil {
return c.CallbackQuery.Message.MessageID
}
return 0
}
// ============ Telegram API Methods ============
// SendMessage basit metin mesajı gönderir
func (c *Context) SendMessage(text string) error {
return c.Bot.SendMessage(c.ChatID, text)
}
// SendMessageWithKeyboard inline keyboard ile mesaj gönderir
func (c *Context) SendMessageWithKeyboard(text string, keyboard *InlineKeyboardMarkup) error {
endpoint := fmt.Sprintf("%s/sendMessage", c.Bot.BaseURL)
payload := map[string]interface{}{
"chat_id": c.ChatID,
"text": text,
"reply_markup": keyboard,
}
jsonData, err := json.Marshal(payload)
if err != nil {
return err
}
resp, err := c.Bot.Client.Post(endpoint, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("telegram API hatası: %d", resp.StatusCode)
}
return nil
}
// EditMessage mesajı düzenler
func (c *Context) EditMessage(messageID int, text string) error {
return c.Bot.EditMessage(c.ChatID, messageID, text)
}
// EditMessageWithKeyboard mesajı keyboard ile birlikte düzenler
func (c *Context) EditMessageWithKeyboard(messageID int, text string, keyboard *InlineKeyboardMarkup) error {
endpoint := fmt.Sprintf("%s/editMessageText", c.Bot.BaseURL)
payload := map[string]interface{}{
"chat_id": c.ChatID,
"message_id": messageID,
"text": text,
"reply_markup": keyboard,
}
jsonData, err := json.Marshal(payload)
if err != nil {
return err
}
resp, err := c.Bot.Client.Post(endpoint, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("telegram API hatası: %d", resp.StatusCode)
}
return nil
}
// DeleteMessage mesajı siler
func (c *Context) DeleteMessage(messageID int) error {
return c.Bot.DeleteMessage(c.ChatID, messageID)
}
// AnswerCallback callback sorgusu yanıtlar
func (c *Context) AnswerCallback(text string, alert bool) error {
if c.CallbackQuery == nil {
return fmt.Errorf("callback query yok")
}
endpoint := fmt.Sprintf("%s/answerCallbackQuery", c.Bot.BaseURL)
data := url.Values{}
data.Set("callback_query_id", c.CallbackQuery.ID)
data.Set("text", text)
if alert {
data.Set("show_alert", "true")
}
resp, err := c.Bot.Client.PostForm(endpoint, data)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}