I'm building a website that lets users add new items in their profiles. Users can also do the same using Discord bot's commands. In both cases the Discord bot sends a message New item added by @user
in a certain channel.
I'm thinking of how to design the application. Should I have the POST
addItem
endpoint on the website's server which the bot would call?
onNewDiscordMessage():
if (addItemCommand):
sendPostRequest(websiteUrl, data)
sendMessageInChannel("New item added!")
But then the website would also need to have the message be sent in the Discord channel. Should I make the Discord
bot a web app too? Have the bot be imported as a dependency and be manageable using POST
endpoint?
POST("send_discord_message")
void sendMessage(String message)
Or should I use RabbitMQ/Kafka queues for this? Or websockets/webhooks?
I'm building a website that lets users add new items in their profiles. Users can also do the same using Discord bot's commands. In both cases the Discord bot sends a message New item added by @user
in a certain channel.
I'm thinking of how to design the application. Should I have the POST
addItem
endpoint on the website's server which the bot would call?
onNewDiscordMessage():
if (addItemCommand):
sendPostRequest(websiteUrl, data)
sendMessageInChannel("New item added!")
But then the website would also need to have the message be sent in the Discord channel. Should I make the Discord
bot a web app too? Have the bot be imported as a dependency and be manageable using POST
endpoint?
POST("send_discord_message")
void sendMessage(String message)
Or should I use RabbitMQ/Kafka queues for this? Or websockets/webhooks?
Share Improve this question asked Mar 9 at 17:21 parsecerparsecer 5,15020 gold badges86 silver badges167 bronze badges2 Answers
Reset to default 1Addition to profile
Server has an API POST /profile/items
which adds an item to the profile.
You can call the same API from the website or Discord bot, it'll work.
Recommendation: Expose separate APIs for both clients (you can call the same function internally), because
- Authentication is different for the website (JWT/session) vs the Discord bot (maybe an authToken)
- List of allowed APIs for the website vs the Discord bot will vary.
So keeping them separate would be cleaner.
POST /profile/items
for web, POST discord/profile/items
for Discord.
Notification on additions to profile
I don't think you need RabbitMQ/Kafka or websockets for this. Unless you're doing it at scale, hitting >1k calls a minute. Sending a notification is just one HTTP webhook call.
Implementation in order of scale:
- Low scale: Just make an async HTTP call after adding the item
- Medium scale: Add a common async queue in your server (in your programming language, internal. Like
ThreadPool
), and queue up notifications there and let it make the HTTP calls. - High scale: Move the queue out of your server, which is Kafka etc.
In summary, as you scale: start with async requests, add an internal queue, move the queue outside the server.
Fwiw I already send Discord notifications for my product, it's only a few lines of Python code:
def __send_discord_message(*, message: str):
webhook_url = "https://discord/api/webhooks/xxx/xxx"
payload = {"content": message}
response = requests.post(webhook_url, json=payload)
if response.status_code < 200 or response.status_code > 299:
raise ValueError(f"Couldn't send Discord msg. {response.text=}, {response.status_code=}")
Lets break it down step by step.
Let's define some operation xyz that the user can perform on the website.
You want a user to do xyz operation via a bot.
You want a sort of acknowledgement that xyz operation has been completed
[Help me understand]: How "bot" sends the notif that xyz is succesful? It should rather be the point where xyz operation has been performed, right?
I will make an assumption that your website [referred to as server now onwards] will send the notif to channel.
Now how exactly should we make it?
You do not want your bot to do any sort of heavy lifting. For example, checking if there did exist an error, is not a responsibility of bot. Why?
Because the bot is not a "client". Its just someone who communicates to your server "Hey, I received xyz message".
All sort of parsing shall happen within the server.
Another reason is, this makes the bot extensible, you do not need to change code of the bot to perform abc operation.
In my belief [Design is subjective]. The addition of profiles should be async. So, kafka/rmq over webhook/websocket. Why?
I am not sure, how retry-ability will work with webhook/socket. But I am sure its better with kafka and rmq, since they have dead-letter-queue. Retry-ability is important since, in production environments, connections are prone to break or operation is prone to fail etc. You do not want to loose a message with no logs.
The nature of business is async. Once your bot communicates, "hey something happened". It has no business to know what happened with "hey something".
Any further clarifications are welcome.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744864597a4597905.html
评论列表(0条)