Initial commit

This commit is contained in:
eleksir
2025-06-08 04:32:16 +03:00
parent c1ad1d3925
commit be6c1b0b21
7 changed files with 185 additions and 26 deletions

View File

@@ -2,18 +2,82 @@
GOOPTS=CGO_ENABLED=0
BUILDOPTS=-ldflags="-s -w" -a -gcflags=all=-l -trimpath -buildvcs=false
MYNAME=bunny-bot
BINARY=$MYNAME
RMCMD=rm -rf
# На windows имя бинарника может зависеть не только от платформы, но и от выбранной цели, для linux-а суффикс .exe
# не нужен
ifeq ($(OS),Windows_NT)
ifdef GOOS
ifeq ($(GOOS),windows)
BINARY=${MYNAME}.exe
else # not ifeq ($(GOOS),windows)
BINARY=${MYNAME}
endif # ifeq ($(GOOS),windows)
else # not ifdef GOOS
BINARY=${MYNAME}.exe
endif # ifdef GOOS
ifeq ($(SHELL), sh.exe)
RMCMD=DEL /Q /F
endif
endif
# Явно определяем символ новой строки, чтобы избежать неоднозначности на windows
define IFS
endef
all: clean build
build:
${GOOPTS} go build ${BUILDOPTS} -o bunny-bot ./cmd/bunny-bot
ifeq ($(OS),Windows_NT)
# Looks like on windows gnu make explicitly set SHELL to sh.exe, if it was not set.
ifeq ($(SHELL), sh.exe)
# # Vanilla cmd.exe / powershell.
SET "CGO_ENABLED=0"
go build ${BUILDOPTS} -o ${BINARY} ./cmd/${MYNAME}
else ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
# # git-bash
CGO_ENABLED=0 go build ${BUILDOPTS} -o ${BINARY} ./cmd/${MYNAME}
else # not ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
# # Some other shell.
# # TODO: handle it.
$(info "-- Dunno how to handle this ${SHELL}")
endif # ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
else # not ($(OS),Windows_NT)
CGO_ENABLED=0 go build ${BUILDOPTS} -o ${BINARY} ./cmd/${MYNAME}
endif # ifeq ($(OS),Windows_NT)
clean:
rm -rf bunny-bot
ifeq ($(OS),Windows_NT)
ifeq ($(SHELL),sh.exe)
# # Vanilla cmd.exe / powershell.
if exist ${BINARY} ${RMCMD} ${BINARY}
else # not ifeq ($(SHELL),sh.exe)
${RMCMD} ./${BINARY}
endif # ifeq ($(SHELL),sh.exe)
else # not ifeq ($(OS),Windows_NT)
${RMCMD} ./${BINARY}
endif
upgrade:
rm -rf vendor
go get -u -t -tool ./...
ifeq ($(OS),Windows_NT)
ifeq ($(SHELL),sh.exe)
# # Vanilla cmd.exe / powershell.
if exist vendor DEL /F /S /Q vendor >nul
else # not ifeq ($(SHELL),sh.exe)
# # git-bash
$(RM) -r vendor
endif # ifeq ($(SHELL),sh.exe)
else # not ifeq ($(OS),Windows_NT)
$(RM) -r vendor
endif
go get -d -u ./...
go mod tidy
go mod vendor

View File

@@ -19,6 +19,8 @@ func leftChatMember(msg *echotron.Update) {
msg.Message.Chat.ID,
)
// Save people that left chat to pebbledb. To preserve their state.
// TODO: Remove items on chatMemberUpdated event.
if err := Cfg.SaveKeyValue(
"LeftMembers",
fmt.Sprintf("%d", msg.Message.Chat.ID),

32
internal/moon/photo.go Normal file
View File

@@ -0,0 +1,32 @@
package moon
import "github.com/NicoNex/echotron/v3"
func photo(msg *echotron.Update) {
if msg.Message.Text != "" {
Log.Infof(
"User %s %s (username = %s, id = %d) sent %d photos to %s %s (%d) with text: %s",
msg.Message.From.FirstName,
msg.Message.From.LastName,
msg.Message.From.Username,
msg.Message.From.ID,
len(msg.Message.Photo),
msg.Message.Chat.Type,
msg.Message.Chat.Title,
msg.Message.Chat.ID,
msg.Message.Text,
)
} else {
Log.Infof(
"User %s %s (username = %s, id = %d) sent %d photos to %s %s (%d)",
msg.Message.From.FirstName,
msg.Message.From.LastName,
msg.Message.From.Username,
msg.Message.From.ID,
len(msg.Message.Photo),
msg.Message.Chat.Type,
msg.Message.Chat.Title,
msg.Message.Chat.ID,
)
}
}

49
internal/moon/reaction.go Normal file
View File

@@ -0,0 +1,49 @@
package moon
import "github.com/NicoNex/echotron/v3"
func reaction(msg *echotron.Update) {
var reactionString string
for _, item := range msg.MessageReaction.NewReaction {
if item.Type == "Emoji" {
reactionString += item.Emoji
} else if item.Type == "CustomEmoji" {
reactionString += item.CustomEmoji
}
}
if reactionString != "" {
Log.Infof(
"User %s %s (username = %s, id = %d) set reaction %s to chat %s %s (%d)",
msg.MessageReaction.User.FirstName,
msg.MessageReaction.User.LastName,
msg.MessageReaction.User.Username,
msg.MessageReaction.User.ID,
reactionString,
msg.MessageReaction.Chat.Type,
msg.MessageReaction.Chat.Title,
msg.MessageReaction.Chat.ID,
)
} else {
for _, item := range msg.MessageReaction.OldReaction {
if item.Type == "Emoji" {
reactionString += item.Emoji
} else if item.Type == "CustomEmoji" {
reactionString += item.CustomEmoji
}
}
Log.Infof(
"User %s %s (username = %s, id = %d) removed reaction %s to chat %s %s (%d)",
msg.MessageReaction.User.FirstName,
msg.MessageReaction.User.LastName,
msg.MessageReaction.User.Username,
msg.MessageReaction.User.ID,
reactionString,
msg.MessageReaction.Chat.Type,
msg.MessageReaction.Chat.Title,
msg.MessageReaction.Chat.ID,
)
}
}

View File

@@ -0,0 +1,16 @@
package moon
import (
"github.com/NicoNex/echotron/v3"
"github.com/davecgh/go-spew/spew"
)
func reactionCount(msg *echotron.Update) {
Log.Infof(
"Reaction count event in %s %s (%d): %",
msg.MessageReaction.Chat.Type,
msg.MessageReaction.Chat.Title,
msg.MessageReaction.Chat.ID,
spew.Sdump(msg),
)
}

View File

@@ -72,6 +72,24 @@ func telegramMsgParser(msg *echotron.Update) {
return
}
if msg.MessageReaction != nil {
reaction(msg)
return
}
if msg.MessageReactionCount != nil {
reactionCount(msg)
return
}
if msg.Message != nil && msg.Message.Photo != nil {
photo(msg)
return
}
Log.Debug(spew.Sdump(msg))
}

View File

@@ -1,9 +1,6 @@
package moon
import (
"fmt"
"time"
"github.com/NicoNex/echotron/v3"
)
@@ -40,25 +37,6 @@ func textMessage(msg *echotron.Update) {
msg.Message.Text,
)
}
// Update database of new members :)
// Try to get timestamp when this member joined to chat
if joinTimestampString := Cfg.GetValue(
"NewMembers",
fmt.Sprintf("%d", msg.Message.Chat.ID),
fmt.Sprintf("%d", msg.Message.From.ID),
); joinTimestampString == "" {
if err := Cfg.SaveKeyValue(
"NewMembers",
fmt.Sprintf("%d", msg.Message.Chat.ID),
fmt.Sprintf("%d", msg.Message.From.ID),
fmt.Sprintf("%d", time.Now().Unix()),
); err != nil {
Log.Error(err)
} else {
Log.Infof("Info about new userid %d saved to new users db", msg.Message.From.ID)
}
}
}
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */