Skip to content

Commit

Permalink
feat(bot): export channel feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
indes committed Nov 9, 2019
1 parent f2c2d0c commit 318c229
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
51 changes: 45 additions & 6 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,50 @@ func makeHandle() {
})

B.Handle("/export", func(m *tb.Message) {
sourceList, err := model.GetSourcesByUserID(m.Chat.ID)

if err != nil {
log.Println(err.Error())
_, _ = B.Send(m.Chat, fmt.Sprintf("导出失败"))
return
mention := GetMentionFromMessage(m)
var sourceList []model.Source
var err error
if mention == "" {

sourceList, err = model.GetSourcesByUserID(m.Chat.ID)
if err != nil {
log.Println(err.Error())
_, _ = B.Send(m.Chat, fmt.Sprintf("导出失败"))
return
}
} else {
channelChat, err := B.ChatByID(mention)

if err != nil {
_, _ = B.Send(m.Chat, "error")
return
}

adminList, err := B.AdminsOf(channelChat)
if err != nil {
_, _ = B.Send(m.Chat, "error")
return
}

senderIsAdmin := false
for _, admin := range adminList {
if m.Sender.ID == admin.User.ID {
senderIsAdmin = true
}
}

if !senderIsAdmin {
_, _ = B.Send(m.Chat, fmt.Sprintf("非频道管理员无法执行此操作"))
return
}

sourceList, err = model.GetSourcesByUserID(channelChat.ID)
if err != nil {
log.Println(err.Error())
_, _ = B.Send(m.Chat, fmt.Sprintf("导出失败"))
return
}
}

if len(sourceList) == 0 {
Expand All @@ -214,13 +252,14 @@ func makeHandle() {
return
}
opmlFile := &tb.Document{File: tb.FromReader(strings.NewReader(opmlStr))}
opmlFile.FileName = "subscriptions.opml"
opmlFile.FileName = fmt.Sprintf("subscriptions_%d.opml", time.Now().Unix())
_, err = B.Send(m.Chat, opmlFile)

if err != nil {
_, _ = B.Send(m.Chat, fmt.Sprintf("导出失败"))
log.Println("[export]", err)
}

})

B.Handle("/sub", func(m *tb.Message) {
Expand Down
12 changes: 12 additions & 0 deletions bot/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ func HasAdminType(t tb.ChatType) bool {
return false
}

func GetMentionFromMessage(m *tb.Message) (mention string) {
for _, entity := range m.Entities {
if entity.Type == tb.EntityMention {
if mention == "" {
mention = m.Text[entity.Offset : entity.Offset+entity.Length]

}
}
}
return
}

func GetUrlAndMentionFromMessage(m *tb.Message) (url string, mention string) {
for _, entity := range m.Entities {
if entity.Type == tb.EntityMention {
Expand Down

0 comments on commit 318c229

Please sign in to comment.