AlgoMaster Logo
AlgoMasterImplement Notification Adaptersmedium

Implement Notification Adapters

medium

Imagine adding notifications to an application that has already accumulated three vendor SDKs. Slack wants a channel, some text, and a bot flag. Teams expects a card title, a body, and a webhook URL. Discord asks for a numeric channel id, the message, and a text-to-speech flag. The application, meanwhile, just wants to say: “send this message to this recipient.”

Your task is deliberately focused: implement only SlackAdapter, TeamsAdapter, and DiscordAdapter. The common NotificationSender contract, all three service clients, and the NotificationHub test harness are already implemented in the starter code.

Every adapter implements send(recipient, message) and translates that call to one vendor API:

  • SlackAdapter(client) stores a SlackClient and calls postMessage(recipient, message, true).
  • TeamsAdapter(webhook, webhookUrl) stores both values and calls sendCard(recipient, message, webhookUrl).
  • DiscordAdapter(bot, channelId) stores both values and calls sendMessage(channelId, message, false). The shared recipient argument is not used for Discord because its destination was fixed when the adapter was created.

The service clients return the line they would have posted:

  • SlackClient.postMessage(channel, text, asBot) returns Slack -> #<channel>: <text> (bot=<asBot>)
  • TeamsWebhook.sendCard(title, body, webhookUrl) returns Teams -> <webhookUrl>: [<title>] <body>
  • DiscordBot.sendMessage(channelId, content, tts) returns Discord -> channel <channelId>: <content> (tts=<tts>)

Slack always posts as a bot, Discord never uses text to speech, and a Teams adapter keeps its webhook URL for later sends. Those service-specific details belong inside the adapters, not in the hub.

The provided NotificationHub registers your adapters, keeps them in one NotificationSender collection, validates indexes, broadcasts in registration order, and counts successful sends. The examples use hub operations because it is the public test harness; your code changes belong only inside the three adapter classes.

Example 1:

Input:

Output:

Explanation: One send call per channel produces three differently shaped lines. The hub passed a recipient and a message, and each adapter supplied everything else its service wanted.

Example 2:

Input:

Output:

Explanation: broadcast reaches both channels in registration order and counts two sends. The last call names index 5, which holds no channel, so it returns UNKNOWN.

Constraints

  • 1 <= webhookUrl.length <= 60
  • 1 <= recipient.length <= 30
  • 1 <= message.length <= 60
  • 0 <= channelId <= 1000000000
  • -20 <= index <= 20
  • At most 100 calls in total are made across all methods.

Starter Code

Only the three adapter classes are unfinished. Define each complete adapter at its marked location. The shared contract, vendor clients, and hub are already implemented; do not rewrite them.

How the design is graded

needs 7/10 to pass
  • One shared sender contract

    Full marks when `SlackAdapter`, `TeamsAdapter`, and `DiscordAdapter` all implement the exact notification sender contract while wrapping their respective vendor clients. Lose points heavily when an adapter changes the shared signature, extends a client, or bypasses delegation by building output itself.

  • Correct argument translation

    Full marks when Slack passes recipient, message, and `true`; Teams passes recipient as title, message as body, and its stored webhook URL; and Discord passes its stored channel id, message, and `false`. Lose points for swapped arguments, missing flags, or using recipient as Discord's destination.

  • Stored configuration and focused scope

    Full marks when Teams retains its webhook and URL, Discord retains its bot and channel id, Slack retains its client, and every send simply delegates and returns the vendor result. Lose points for recreating dependencies per send, leaking service-specific state into the supplied hub, or printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new NotificationHub()null
addSlack()0
addTeams("https://webhook.teams.com/abc")1
addDiscord(123456789)2
send(0, "general", "Build succeeded")"Slack -> #general: Build succeeded (bot=true)"
send(1, "Deployment", "Service deployed to production")"Teams -> https://webhook.teams.com/abc: [Deployment] Service deployed to production"
send(2, "alerts", "CPU usage above 90%")"Discord -> channel 123456789: CPU usage above 90% (tts=false)"
sendCount()3

One `send` call per channel produces three differently shaped lines. The hub passed a recipient and a message, and each adapter supplied everything else its service wanted.

Run checks these cases. Submit also runs a larger hidden set.