-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
151 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package constant | ||
|
||
const ( | ||
KeyBot = "keyNotifyBot" | ||
KeyChatID = "keyChatID" | ||
KeyUserList = "keyUserList" | ||
KeyUserList = "keyUserList" | ||
KeyChannelHeader = "channel" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package model | ||
|
||
type SendMessageRequest struct { | ||
Channel string `json:"channel"` | ||
Message string `json:"message"` | ||
MessageType string `json:"message_type"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package sender | ||
|
||
import ( | ||
"context" | ||
|
||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" | ||
"github.com/xxxsen/common/errs" | ||
) | ||
|
||
type BotMsgSender struct { | ||
name string | ||
chatid int64 | ||
bot *tgbotapi.BotAPI | ||
} | ||
|
||
func NewBotMsgSender(name string, chatid int64, token string) (*BotMsgSender, error) { | ||
bot, err := tgbotapi.NewBotAPI(token) | ||
if err != nil { | ||
return nil, errs.Wrap(errs.ErrUnknown, "init bot fail", err) | ||
} | ||
return &BotMsgSender{name, chatid, bot}, nil | ||
} | ||
|
||
func (c *BotMsgSender) Name() string { | ||
return c.name | ||
} | ||
|
||
func (c *BotMsgSender) SendMessage(ctx context.Context, mode string, message string) error { | ||
msg := tgbotapi.NewMessage(c.chatid, message) | ||
if len(mode) != 0 { | ||
msg.ParseMode = mode | ||
} | ||
_, err := c.bot.Send(msg) | ||
if err != nil { | ||
return errs.Wrap(errs.ErrIO, "send msg fail", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package sender | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/xxxsen/common/errs" | ||
) | ||
|
||
var glgp *GroupMsgSender | ||
|
||
func InitGlobalGroupMessageSender(chsm map[string]IMessageSender, def IMessageSender) { | ||
glgp = NewGroupMsgSender(chsm, def) | ||
} | ||
|
||
type GroupMsgSender struct { | ||
chs map[string]IMessageSender | ||
def IMessageSender | ||
} | ||
|
||
func NewGroupMsgSender(chsm map[string]IMessageSender, def IMessageSender) *GroupMsgSender { | ||
return &GroupMsgSender{ | ||
chs: chsm, def: def, | ||
} | ||
} | ||
|
||
func (c *GroupMsgSender) SendMessage(ctx context.Context, name string, mode string, message string) error { | ||
s, ok := c.chs[name] | ||
if !ok { | ||
s = c.def | ||
} | ||
if s == nil { | ||
return errs.New(errs.ErrServiceInternal, "no sender found") | ||
} | ||
return s.SendMessage(ctx, mode, message) | ||
} | ||
|
||
func SendMessageByChannel(ctx context.Context, name string, mode string, message string) error { | ||
return glgp.SendMessage(ctx, name, mode, message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package sender | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type IMessageSender interface { | ||
Name() string | ||
SendMessage(ctx context.Context, mode string, message string) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters