Skip to content

Commit

Permalink
fix teams bot
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikus1993 committed May 6, 2022
1 parent 4185348 commit 3507155
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
59 changes: 54 additions & 5 deletions cmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/dominikus1993/dev-news-bot/internal/console"
"github.com/dominikus1993/dev-news-bot/internal/discord"
"github.com/dominikus1993/dev-news-bot/internal/microsoftteams"
"github.com/dominikus1993/dev-news-bot/internal/mongo"
"github.com/dominikus1993/dev-news-bot/internal/parser/devto"
"github.com/dominikus1993/dev-news-bot/internal/parser/dotnetomaniak"
Expand All @@ -17,11 +18,58 @@ import (
log "github.com/sirupsen/logrus"
)

type notifiers struct {
discord *discord.DiscordWebhookNotifier
pprint *console.PPrintNotifier
teams *microsoftteams.TeamsWebhookNotifier
}

func createNotifiers(cmd *ParseArticlesAndSendIt) (notifiers, error) {
var notifiers notifiers
if cmd.dicordWebhookId != "" && cmd.discordWebhookToken != "" {
discordn, err := discord.NewDiscordWebhookNotifier(cmd.dicordWebhookId, cmd.discordWebhookToken)
if err != nil {
return notifiers, err
}
notifiers.discord = discordn
}
if cmd.teamsWebhookUrl != "" {
teamsn, err := microsoftteams.NewDiscordWebhookNotifier(cmd.teamsWebhookUrl)
if err != nil {
return notifiers, err
}
notifiers.teams = teamsn
}
notifiers.pprint = console.NewPPrintNotifier()
return notifiers, nil
}

func (n notifiers) createBroadcaster() notifications.Broadcaster {
var notifiers []notifications.Notifier
if n.discord != nil {
notifiers = append(notifiers, n.discord)
}
if n.pprint != nil {
notifiers = append(notifiers, n.pprint)
}
if n.teams != nil {
notifiers = append(notifiers, n.teams)
}
return notifications.NewBroadcaster(notifiers...)
}

func (n notifiers) close() {
if n.discord != nil {
n.discord.Close()
}
}

type ParseArticlesAndSendIt struct {
quantity int
dicordWebhookId string
discordWebhookToken string
mongoConnectionString string
teamsWebhookUrl string
}

func (*ParseArticlesAndSendIt) Name() string { return "parse" }
Expand All @@ -35,6 +83,7 @@ func (p *ParseArticlesAndSendIt) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.dicordWebhookId, "dicord-webhook-id", "", "dicord webhook id")
f.StringVar(&p.discordWebhookToken, "discord-webhook-token", "", "discord webhook token")
f.StringVar(&p.mongoConnectionString, "mongo-connection-string", "", "mongo connection string")
f.StringVar(&p.teamsWebhookUrl, "teams-webhook-url", "", "teams webhook url")
}

func (p *ParseArticlesAndSendIt) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
Expand All @@ -50,14 +99,14 @@ func (p *ParseArticlesAndSendIt) Execute(ctx context.Context, f *flag.FlagSet, _
dotnetomaniakParser := dotnetomaniak.NewDotnetoManiakParser()
repo := mongo.NewMongoArticlesRepository(mongodbClient)
articlesProvider := providers.NewArticlesProvider(repo, hackernewsParser, dotnetomaniakParser, devtoParser)
discord, err := discord.NewDiscordWebhookNotifier(p.dicordWebhookId, p.discordWebhookToken)
notifiers, err := createNotifiers(p)
if err != nil {
log.WithError(err).Error("error creating discord notifier")
log.WithError(err).Error("can't create notifiers")
return subcommands.ExitFailure
}
defer discord.Close()
pprint := console.NewPPrintNotifier()
bradcaster := notifications.NewBroadcaster(pprint, discord)
defer notifiers.close()

bradcaster := notifiers.createBroadcaster()
usecase := usecase.NewParseArticlesAndSendItUseCase(articlesProvider, repo, bradcaster)

err = usecase.Execute(ctx, p.quantity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func NewDiscordWebhookNotifier(webhookUrl string) (*TeamsWebhookNotifier, error)
func createDiscordEmbedsFromArticles(articles []model.Article) goteamsnotify.MessageCard {
msgCard := goteamsnotify.NewMessageCard()
msgCard.Title = "Witam serdecznie, oto nowe newsy"
msgCard.Text = "Oto nowe newsy"
for _, article := range articles {
pa, _ := goteamsnotify.NewMessageCardPotentialAction(
goteamsnotify.PotentialActionOpenURIType,
Expand Down

0 comments on commit 3507155

Please sign in to comment.