Skip to content

Commit

Permalink
fix(set): button action error
Browse files Browse the repository at this point in the history
  • Loading branch information
indes committed May 26, 2019
1 parent 85d9faf commit 271f31f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
42 changes: 23 additions & 19 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func init() {
poller := &tb.LongPoller{Timeout: 10 * time.Second}
spamProtected := tb.NewMiddlewarePoller(poller, func(upd *tb.Update) bool {

if !CheckAdmin(upd.Message) {
//if upd.Message == nil {
// return true
//}
if !CheckAdmin(upd) {
return false
}

Expand Down Expand Up @@ -311,26 +314,27 @@ func makeHandle() {
if HasAdminType(m.Chat.Type) {
_, _ = B.Send(m.Chat, "Group和Channel暂时不支持该功能")
} else {
sources, _ := model.GetSourcesByUserID(m.Chat.ID)

var replyButton []tb.ReplyButton
replyKeys := [][]tb.ReplyButton{}
for _, source := range sources {
// 添加按钮
text := fmt.Sprintf("%s %s", source.Title, source.Link)
replyButton = []tb.ReplyButton{
tb.ReplyButton{Text: text},
}
replyKeys = append(replyKeys, replyButton)
}
sources, _ := model.GetSourcesByUserID(m.Chat.ID)

var replyButton []tb.ReplyButton
replyKeys := [][]tb.ReplyButton{}
for _, source := range sources {
// 添加按钮
text := fmt.Sprintf("%s %s", source.Title, source.Link)
replyButton = []tb.ReplyButton{
tb.ReplyButton{Text: text},
}
_, err := B.Send(m.Chat, "请选择你要设置的源", &tb.ReplyMarkup{
ForceReply: true,
ReplyKeyboard: replyKeys,
})
replyKeys = append(replyKeys, replyButton)
}
_, err := B.Send(m.Chat, "请选择你要设置的源", &tb.ReplyMarkup{
ForceReply: true,
ReplyKeyboard: replyKeys,
})

if err == nil {
UserState[m.Chat.ID] = fsm.Set
}
if err == nil {
UserState[m.Chat.ID] = fsm.Set
}

})
Expand Down Expand Up @@ -434,7 +438,7 @@ func makeHandle() {
if err == nil {
_, _ = B.Send(
m.Chat,
fmt.Sprintf("频道 [%s](https://t.me/%s) 退订成功 [%s](%s)", channelChat.Title, channelChat.Username, source.Title, source.Link),
fmt.Sprintf("频道 [%s](https://t.me/%s) 退订 [%s](%s) 成功", channelChat.Title, channelChat.Username, source.Title, source.Link),
&tb.SendOptions{
DisableWebPagePreview: true,
ParseMode: tb.ModeMarkdown,
Expand Down
36 changes: 25 additions & 11 deletions bot/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,34 @@ func BroadSourceError(source *model.Source) {
}
}

func CheckAdmin(m *tb.Message) bool {
if m == nil {
return false
}
if HasAdminType(m.Chat.Type) {
adminList, _ := B.AdminsOf(m.Chat)
for _, admin := range adminList {
if admin.User.ID == m.Sender.ID {
return true
func CheckAdmin(upd *tb.Update) bool {

if upd.Message != nil {
if HasAdminType(upd.Message.Chat.Type) {
adminList, _ := B.AdminsOf(upd.Message.Chat)
for _, admin := range adminList {
if admin.User.ID == upd.Message.Sender.ID {
return true
}
}
return false
}

return true
} else if upd.Callback != nil {
if HasAdminType(upd.Callback.Message.Chat.Type) {
adminList, _ := B.AdminsOf(upd.Callback.Message.Chat)
for _, admin := range adminList {
if admin.User.ID == upd.Callback.Sender.ID {
return true
}
}
return false
}
return false

return true
}
return true
return false
}

func HasAdminType(t tb.ChatType) bool {
Expand Down

0 comments on commit 271f31f

Please sign in to comment.