Browse Source

New package name and fixed imports.

master
Syfaro 1 year ago
parent
commit
c909d39c8c

+ 2
- 2
.gitignore View File

@@ -1,2 +1,2 @@
1
-selectionsbot
2
-storage.db
1
+telegram-selectionsbot
2
+*.db

+ 7
- 6
bot.go View File

@@ -6,13 +6,13 @@ import (
6 6
 	"github.com/jmoiron/sqlx"
7 7
 	_ "github.com/mattn/go-sqlite3"
8 8
 
9
-	"github.com/syfaro/finch"
10
-	_ "github.com/syfaro/finch/commands/cancel"
11
-	_ "github.com/syfaro/finch/commands/help"
9
+	"github.com/Syfaro/finch"
10
+	_ "github.com/Syfaro/finch/commands/cancel"
11
+	_ "github.com/Syfaro/finch/commands/help"
12 12
 
13
-	_ "github.com/syfaro/selectionsbot/commands/manage"
14
-	_ "github.com/syfaro/selectionsbot/commands/start"
15
-	"github.com/syfaro/selectionsbot/database"
13
+	_ "github.com/Syfaro/telegram-selectionsbot/commands/manage"
14
+	_ "github.com/Syfaro/telegram-selectionsbot/commands/start"
15
+	"github.com/Syfaro/telegram-selectionsbot/database"
16 16
 )
17 17
 
18 18
 func main() {
@@ -30,6 +30,7 @@ func main() {
30 30
 			id integer primary key,
31 31
 			user_id integer not null,
32 32
 			chat_id integer not null,
33
+			message_id integer,
33 34
 			title text,
34 35
 			active integer
35 36
 		);

+ 4
- 3
commands/manage/count_items.go View File

@@ -3,10 +3,11 @@ package commands
3 3
 import (
4 4
 	"bytes"
5 5
 	"database/sql"
6
-	"github.com/syfaro/finch"
7
-	"github.com/syfaro/selectionsbot/database"
8
-	"gopkg.in/telegram-bot-api.v4"
9 6
 	"strconv"
7
+
8
+	"github.com/Syfaro/finch"
9
+	"github.com/Syfaro/telegram-selectionsbot/database"
10
+	"github.com/go-telegram-bot-api/telegram-bot-api"
10 11
 )
11 12
 
12 13
 func init() {

+ 219
- 5
commands/manage/create_selection.go View File

@@ -1,12 +1,14 @@
1 1
 package commands
2 2
 
3 3
 import (
4
+	"bytes"
4 5
 	"database/sql"
5
-	"github.com/syfaro/finch"
6
-	"github.com/syfaro/selectionsbot/database"
7
-	"gopkg.in/telegram-bot-api.v4"
8 6
 	"strconv"
9 7
 	"strings"
8
+
9
+	"github.com/Syfaro/finch"
10
+	"github.com/Syfaro/telegram-selectionsbot/database"
11
+	"github.com/go-telegram-bot-api/telegram-bot-api"
10 12
 )
11 13
 
12 14
 func init() {
@@ -98,10 +100,222 @@ func (cmd createSelection) ExecuteWaiting(message tgbotapi.Message) error {
98 100
 		num++
99 101
 	}
100 102
 
101
-	return cmd.QuickReply(message, "Added "+strconv.Itoa(num)+" items to selection!")
103
+	err = cmd.QuickReply(message, "Added "+strconv.Itoa(num)+" items to selection!")
104
+	if err != nil {
105
+		return err
106
+	}
107
+
108
+	msg, err := cmd.selectionMessage(message.Chat.ID)
109
+	if err != nil {
110
+		return err
111
+	}
112
+
113
+	m, err := cmd.API.Send(msg)
114
+
115
+	database.DB.Exec(`update selection set message_id = ? where id = ?`, m.MessageID, selection.ID)
116
+
117
+	return err
118
+}
119
+
120
+func (createSelection) ShouldExecuteCallback(q tgbotapi.CallbackQuery) bool {
121
+	return strings.HasPrefix(q.Data, "v")
122
+}
123
+
124
+func (createSelection) selectionMessage(chatID int64) (tgbotapi.MessageConfig, error) {
125
+	msg := tgbotapi.MessageConfig{}
126
+	msg.ChatID = chatID
127
+
128
+	var selection database.Selection
129
+	err := database.DB.Get(&selection, `
130
+		select
131
+			*
132
+		from
133
+			selection
134
+		where
135
+			chat_id = $1
136
+		order by
137
+			id desc
138
+	`, chatID)
139
+	if err != nil {
140
+		return msg, err
141
+	}
142
+
143
+	var items []database.SelectionItem
144
+	err = database.DB.Select(&items, `
145
+		select
146
+			*
147
+		from
148
+			selection_item
149
+		where
150
+			selection_id = $1
151
+	`, selection.ID)
152
+	if err != nil {
153
+		return msg, err
154
+	}
155
+
156
+	b := bytes.Buffer{}
157
+
158
+	if selection.Title.Valid && selection.Title.String != "" {
159
+		b.WriteString("Selections from ")
160
+		b.WriteString(selection.Title.String)
161
+		b.WriteString("\n\n")
162
+	}
163
+
164
+	for _, item := range items {
165
+		var votes []database.SelectionVote
166
+		err = database.DB.Select(&votes, `
167
+			select
168
+				*
169
+			from
170
+				selection_vote
171
+			where
172
+				selection_id = $1 and
173
+				selection_item_id = $2
174
+		`, selection.ID, item.ID)
175
+		if err != nil {
176
+			return msg, err
177
+		}
178
+
179
+		var users []string
180
+
181
+		for _, vote := range votes {
182
+			var user database.User
183
+			err = database.DB.Get(&user, `
184
+				select
185
+					*
186
+				from
187
+					user
188
+				where
189
+					id = $1
190
+			`, vote.UserID)
191
+			if err != nil {
192
+				return msg, err
193
+			}
194
+
195
+			users = append(users, user.Name)
196
+		}
197
+
198
+		b.WriteString(item.Item)
199
+		b.WriteString(" - ")
200
+		if len(users) == 0 {
201
+			b.WriteString("None")
202
+		} else {
203
+			b.WriteString(strings.Join(users, ", "))
204
+		}
205
+		b.WriteString("\n")
206
+	}
207
+
208
+	msg.Text = b.String()
209
+
210
+	markup := tgbotapi.NewInlineKeyboardMarkup()
211
+
212
+	for _, item := range items {
213
+		data := "v" + strconv.FormatInt(item.ID, 10) + "," + strconv.FormatInt(chatID, 10)
214
+
215
+		markup.InlineKeyboard = append(markup.InlineKeyboard,
216
+			tgbotapi.NewInlineKeyboardRow(
217
+				tgbotapi.NewInlineKeyboardButtonData(item.Item, data)))
218
+	}
219
+
220
+	msg.ReplyMarkup = markup
221
+
222
+	return msg, nil
223
+}
224
+
225
+func (cmd createSelection) ExecuteCallback(callback tgbotapi.CallbackQuery) error {
226
+	strs := strings.Split(strings.TrimPrefix(callback.Data, "v"), ",")
227
+	if len(strs) != 2 {
228
+		return nil
229
+	}
230
+
231
+	itemID, _ := strconv.ParseInt(strs[0], 10, 64)
232
+	chatID, _ := strconv.ParseInt(strs[1], 10, 64)
233
+
234
+	user := database.User{}
235
+	err := user.Load(callback.From.ID)
236
+	if err != nil && err != sql.ErrNoRows {
237
+		return err
238
+	} else if err == sql.ErrNoRows {
239
+		if err = user.Init(callback.From); err != nil {
240
+			return err
241
+		}
242
+	}
243
+
244
+	answer := tgbotapi.CallbackConfig{
245
+		CallbackQueryID: callback.ID,
246
+		ShowAlert:       false,
247
+	}
248
+
249
+	var selection database.Selection
250
+	err = database.DB.Get(&selection, `
251
+		select
252
+			*
253
+		from
254
+			selection
255
+		where
256
+			chat_id = $1 and
257
+			active = 1
258
+	`, chatID)
259
+	if err != nil && err != sql.ErrNoRows {
260
+		return err
261
+	} else if err == sql.ErrNoRows {
262
+		answer.ShowAlert = true
263
+		answer.Text = "Server error"
264
+
265
+		_, err := cmd.API.AnswerCallbackQuery(answer)
266
+		return err
267
+	}
268
+
269
+	var item database.SelectionItem
270
+	err = database.DB.Get(&item, `
271
+		select
272
+			*
273
+		from
274
+			selection_item
275
+		where
276
+			id = $1 and
277
+			selection_id = $2
278
+	`, itemID, selection.ID)
279
+	if err != nil && err != sql.ErrNoRows {
280
+		return err
281
+	} else if err == sql.ErrNoRows {
282
+		answer.ShowAlert = true
283
+		answer.Text = "Server error"
284
+
285
+		_, err := cmd.API.AnswerCallbackQuery(answer)
286
+		return err
287
+	}
288
+
289
+	_, err = database.DB.Exec(`
290
+		delete from
291
+			selection_vote
292
+		where
293
+			user_id = $1 and
294
+			selection_id = $2
295
+	`, user.ID, selection.ID)
296
+	if err != nil {
297
+		return err
298
+	}
299
+
300
+	_, err = database.NewSelectionVote(user.ID, selection.ID, item.ID)
301
+	if err != nil {
302
+		return err
303
+	}
304
+
305
+	msg, _ := cmd.selectionMessage(callback.Message.Chat.ID)
306
+	markup := msg.ReplyMarkup.(tgbotapi.InlineKeyboardMarkup)
307
+
308
+	updateMessage := tgbotapi.NewEditMessageText(callback.Message.Chat.ID, *selection.MessageID, msg.Text)
309
+	updateMessage.ReplyMarkup = &markup
310
+	cmd.API.Send(updateMessage)
311
+
312
+	answer.Text = "Added selection!"
313
+
314
+	_, err = cmd.API.AnswerCallbackQuery(answer)
315
+	return err
102 316
 }
103 317
 
104
-func (cmd createSelection) Help() finch.Help {
318
+func (createSelection) Help() finch.Help {
105 319
 	return finch.Help{
106 320
 		Name:        "Create selection",
107 321
 		Description: "Create a new selection set",

+ 4
- 3
commands/manage/end_selection.go View File

@@ -2,9 +2,10 @@ package commands
2 2
 
3 3
 import (
4 4
 	"database/sql"
5
-	"github.com/syfaro/finch"
6
-	"github.com/syfaro/selectionsbot/database"
7
-	"gopkg.in/telegram-bot-api.v4"
5
+
6
+	"github.com/Syfaro/finch"
7
+	"github.com/Syfaro/telegram-selectionsbot/database"
8
+	"github.com/go-telegram-bot-api/telegram-bot-api"
8 9
 )
9 10
 
10 11
 func init() {

+ 4
- 3
commands/manage/select_item.go View File

@@ -2,9 +2,10 @@ package commands
2 2
 
3 3
 import (
4 4
 	"database/sql"
5
-	"github.com/syfaro/finch"
6
-	"github.com/syfaro/selectionsbot/database"
7
-	"gopkg.in/telegram-bot-api.v4"
5
+
6
+	"github.com/Syfaro/finch"
7
+	"github.com/Syfaro/telegram-selectionsbot/database"
8
+	"github.com/go-telegram-bot-api/telegram-bot-api"
8 9
 )
9 10
 
10 11
 func init() {

+ 4
- 3
commands/manage/selection_list.go View File

@@ -3,10 +3,11 @@ package commands
3 3
 import (
4 4
 	"bytes"
5 5
 	"database/sql"
6
-	"github.com/syfaro/finch"
7
-	"github.com/syfaro/selectionsbot/database"
8
-	"gopkg.in/telegram-bot-api.v4"
9 6
 	"strings"
7
+
8
+	"github.com/Syfaro/finch"
9
+	"github.com/Syfaro/telegram-selectionsbot/database"
10
+	"github.com/go-telegram-bot-api/telegram-bot-api"
10 11
 )
11 12
 
12 13
 func init() {

+ 2
- 2
commands/start/start.go View File

@@ -1,8 +1,8 @@
1 1
 package commands
2 2
 
3 3
 import (
4
-	"github.com/syfaro/finch"
5
-	"gopkg.in/telegram-bot-api.v4"
4
+	"github.com/Syfaro/finch"
5
+	"github.com/go-telegram-bot-api/telegram-bot-api"
6 6
 )
7 7
 
8 8
 var usage string = `How to use this bot:

+ 8
- 6
database/db.go View File

@@ -2,8 +2,9 @@ package database
2 2
 
3 3
 import (
4 4
 	"database/sql"
5
+
6
+	"github.com/go-telegram-bot-api/telegram-bot-api"
5 7
 	"github.com/jmoiron/sqlx"
6
-	"gopkg.in/telegram-bot-api.v4"
7 8
 )
8 9
 
9 10
 var DB *sqlx.DB
@@ -49,11 +50,12 @@ func (u *User) Init(user *tgbotapi.User) error {
49 50
 }
50 51
 
51 52
 type Selection struct {
52
-	ID     int64          `db:"id"`
53
-	UserID int64          `db:"user_id"`
54
-	ChatID int64          `db:"chat_id"`
55
-	Title  sql.NullString `db:"title"`
56
-	Active bool           `db:"active"`
53
+	ID        int64          `db:"id"`
54
+	UserID    int64          `db:"user_id"`
55
+	ChatID    int64          `db:"chat_id"`
56
+	MessageID *int           `db:"message_id"`
57
+	Title     sql.NullString `db:"title"`
58
+	Active    bool           `db:"active"`
57 59
 }
58 60
 
59 61
 func NewSelection(userID, chatID int64) (Selection, error) {

Loading…
Cancel
Save