Skip to content

Commit

Permalink
feat: honor the #nobot tag in channel posts
Browse files Browse the repository at this point in the history
  • Loading branch information
akurilov committed Jun 15, 2024
1 parent 226bb21 commit 0b3eb99
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions service/messages/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,38 @@ type ChanPostHandler struct {
ChansLock *sync.Mutex
}

const tagNoBot = "#nobot"

var errNoAck = errors.New("event was not accepted")

func (cp ChanPostHandler) Publish(tgCtx telebot.Context) (err error) {

tgMsg := tgCtx.Message()
ch := tgCtx.Chat()
chanUserName := ch.Username

var txt string
switch {
case tgMsg.Text != "":
txt = tgMsg.Text
case tgMsg.Caption != "":
txt = tgMsg.Caption
}
for _, w := range strings.Split(txt, " ") {
if w == tagNoBot {
cp.Log.Warn(fmt.Sprintf("Channel %s (%d) post %d contains the %s tag, skipping", chanUserName, ch.ID, tgMsg.ID, tagNoBot))
return
}
}

chanUserId := fmt.Sprintf("@%s", chanUserName)
evt := pb.CloudEvent{
Id: uuid.NewString(),
Source: fmt.Sprintf("https://t.me/%s", chanUserName),
SpecVersion: attrValSpecVersion,
Type: "com.awakari.bot-telegram.v1",
}
if err == nil {
err = toCloudEvent(tgCtx.Message(), tgCtx.Text(), &evt)
}
err = toCloudEvent(tgMsg, tgCtx.Text(), &evt)
if err == nil {
err = cp.getWriterAndPublish(chanUserId, &evt)
if err != nil {
Expand Down

0 comments on commit 0b3eb99

Please sign in to comment.