Update code
This commit is contained in:
parent
2521fd8860
commit
033c33b5e7
1 changed files with 43 additions and 19 deletions
62
main.go
62
main.go
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
@ -11,9 +12,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AdminJoinRequestMsg = "New join #request from <i>%s</i> [<code>%d</code>]\n\nJoin reason: %s"
|
AdminJoinRequestMsg = "New join #request from <i>%s</i> [<code>%d</code>]\n\n<b>Join reason</b>: %s"
|
||||||
AdminApprovedMsg = "✅ Join #request approved for <i>%s</i> [<code>%d</code>]\n\nJoin reason: %s\nApproved by: %s\nApproved at: %s"
|
AdminApprovedMsg = "✅ Join #request approved for <i>%s</i> [<code>%d</code>]\n\n<b>Join reason</b>: %s\n<b>Approved by</b>: %s\n<b>Approved at</b>: %s"
|
||||||
AdminDeclinedMsg = "❌ Join #request declined for <i>%s</i> [<code>%d</code>]\n\nJoin reason: %s\nDeclined by: %s\nDeclined at: %s\nDeclined reason: %s"
|
AdminDeclinedMsg = "❌ Join #request declined for <i>%s</i> [<code>%d</code>]\n\n<b>Join reason</b>: %s\n<b>Declined by</b>: %s\n<b>Declined at</b>: %s\n<b>Declined reason</b>: %s"
|
||||||
|
AdminFailedMsg = "⚠️ Join #request failed for <i>%s</i> [<code>%d</code>]\n\n<b>Join reason</b>: %s\n<b>Failure reason</b>: %s"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ExtendedChatJoinRequest struct {
|
type ExtendedChatJoinRequest struct {
|
||||||
|
|
@ -51,6 +53,8 @@ func main() {
|
||||||
updatesChannel := b.API.GetUpdatesChan(updateConfig)
|
updatesChannel := b.API.GetUpdatesChan(updateConfig)
|
||||||
|
|
||||||
for update := range updatesChannel {
|
for update := range updatesChannel {
|
||||||
|
j, _ := json.MarshalIndent(update, "", " ")
|
||||||
|
log.Printf("Received update: %s", j)
|
||||||
if update.ChatJoinRequest != nil {
|
if update.ChatJoinRequest != nil {
|
||||||
if update.ChatJoinRequest.Chat.ID == b.Config.TargetChatId {
|
if update.ChatJoinRequest.Chat.ID == b.Config.TargetChatId {
|
||||||
b.handleJoinRequest(update.ChatJoinRequest)
|
b.handleJoinRequest(update.ChatJoinRequest)
|
||||||
|
|
@ -171,20 +175,30 @@ func (bot *Bot) handleCallbackQuery(query *api.CallbackQuery) {
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case "approve":
|
case "approve":
|
||||||
r := api.ApproveChatJoinRequestConfig{
|
|
||||||
ChatConfig: api.ChatConfig{
|
|
||||||
ChatID: user.ChatJoinRequest.Chat.ID,
|
|
||||||
},
|
|
||||||
UserID: user.ChatJoinRequest.From.ID,
|
|
||||||
}
|
|
||||||
bot.API.Send(r)
|
|
||||||
|
|
||||||
userString := ""
|
userString := ""
|
||||||
if user.From.UserName != "" {
|
if user.From.UserName != "" {
|
||||||
userString = "@" + user.From.UserName
|
userString = "@" + user.From.UserName
|
||||||
} else {
|
} else {
|
||||||
userString = fmt.Sprintf("%s %s (no username)", user.From.FirstName, user.From.LastName)
|
userString = fmt.Sprintf("%s %s (no username)", user.From.FirstName, user.From.LastName)
|
||||||
}
|
}
|
||||||
|
r := api.ApproveChatJoinRequestConfig{
|
||||||
|
ChatConfig: api.ChatConfig{
|
||||||
|
ChatID: user.ChatJoinRequest.Chat.ID,
|
||||||
|
},
|
||||||
|
UserID: user.ChatJoinRequest.From.ID,
|
||||||
|
}
|
||||||
|
_, e := bot.API.Send(r)
|
||||||
|
if e != nil {
|
||||||
|
edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID,
|
||||||
|
fmt.Sprintf(AdminFailedMsg, userString, user.From.ID, user.JoinReason, "User not found in requests."))
|
||||||
|
edit.ParseMode = api.ModeHTML
|
||||||
|
bot.API.Send(edit)
|
||||||
|
|
||||||
|
callback := api.NewCallback(query.ID, "Join request failed.")
|
||||||
|
bot.API.Request(callback)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID,
|
edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID,
|
||||||
fmt.Sprintf(AdminApprovedMsg,
|
fmt.Sprintf(AdminApprovedMsg,
|
||||||
userString, user.From.ID, user.JoinReason, query.From.String(), time.Now().Format("2006-01-02 15:04:05")))
|
userString, user.From.ID, user.JoinReason, query.From.String(), time.Now().Format("2006-01-02 15:04:05")))
|
||||||
|
|
@ -201,20 +215,30 @@ func (bot *Bot) handleCallbackQuery(query *api.CallbackQuery) {
|
||||||
bot.API.Request(callback)
|
bot.API.Request(callback)
|
||||||
|
|
||||||
case "decline":
|
case "decline":
|
||||||
r := api.DeclineChatJoinRequest{
|
|
||||||
ChatConfig: api.ChatConfig{
|
|
||||||
ChatID: user.ChatJoinRequest.Chat.ID,
|
|
||||||
},
|
|
||||||
UserID: user.ChatJoinRequest.From.ID,
|
|
||||||
}
|
|
||||||
bot.API.Send(r)
|
|
||||||
|
|
||||||
userString := ""
|
userString := ""
|
||||||
if user.From.UserName != "" {
|
if user.From.UserName != "" {
|
||||||
userString = "@" + user.From.UserName
|
userString = "@" + user.From.UserName
|
||||||
} else {
|
} else {
|
||||||
userString = fmt.Sprintf("%s %s (no username)", user.From.FirstName, user.From.LastName)
|
userString = fmt.Sprintf("%s %s (no username)", user.From.FirstName, user.From.LastName)
|
||||||
}
|
}
|
||||||
|
r := api.DeclineChatJoinRequest{
|
||||||
|
ChatConfig: api.ChatConfig{
|
||||||
|
ChatID: user.ChatJoinRequest.Chat.ID,
|
||||||
|
},
|
||||||
|
UserID: user.ChatJoinRequest.From.ID,
|
||||||
|
}
|
||||||
|
_, e := bot.API.Send(r)
|
||||||
|
if e != nil {
|
||||||
|
edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID,
|
||||||
|
fmt.Sprintf(AdminFailedMsg, userString, user.From.ID, user.JoinReason, "User not found in requests."))
|
||||||
|
edit.ParseMode = api.ModeHTML
|
||||||
|
bot.API.Send(edit)
|
||||||
|
|
||||||
|
callback := api.NewCallback(query.ID, "Join request failed.")
|
||||||
|
bot.API.Request(callback)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID,
|
edit := api.NewEditMessageText(bot.Config.AdminChatId, user.JoinRequestMessageID,
|
||||||
fmt.Sprintf(AdminDeclinedMsg,
|
fmt.Sprintf(AdminDeclinedMsg,
|
||||||
userString, user.From.ID, user.JoinReason, query.From.String(), time.Now().Format("2006-01-02 15:04:05"), "(no reason provided, reply to this to send one, prepend with + to also send to user)"))
|
userString, user.From.ID, user.JoinReason, query.From.String(), time.Now().Format("2006-01-02 15:04:05"), "(no reason provided, reply to this to send one, prepend with + to also send to user)"))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue