简体   繁体   English

使用Python Slack bot一次为多个用户提供服务

[英]Serving multiple users at once with a Python Slack bot

I have a bot written in Python running on Amazon EC2 with Django as a framework. 我有一个用Python编写的机器人在Amazon EC2上运行,Django作为框架。 The bot's end goal is to sustain conversations with multiple users on the same Slack team at once. 机器人的最终目标是同时在同一个Slack团队中与多个用户保持对话。 As I understand it, Amazon will handle load-bearing between Slack teams, but I'm trying to figure out how to manage the load within a single Slack. 据我了解,亚马逊将处理Slack团队之间的负载,但我正在试图找出如何在单个Slack中管理负载。

Right now, my bot sits in a busy loop waiting for a single user to respond. 现在,我的机器人坐在忙碌的循环中等待单个用户做出响应。 I've been doing some research on this - is Celery the right tool for the job? 我一直在研究这个问题 - 芹菜是适合这项工作的合适工具吗? Should I split each conversation into a separate thread/task, or maybe have a dispatcher handle new messages? 我应该将每个会话拆分为单独的线程/任务,还是让调度程序处理新消息? Is there a way for Slack to send an interrupt, or am I stuck with while loops? Slack是否有办法发送中断,或者我是否仍然坚持使用while循环?

Thanks for any help/guidance! 感谢您的帮助/指导! I'm pretty new to this. 我对此很陌生。

Edit: I managed to solve this problem by implementing a list of "Conversation" objects pertaining to each user. 编辑:我设法通过实现与每个用户有关的“对话”对象列表来解决这个问题。 These objects save the state of each conversation, so that the bot can pick up where it left off when the user messages again. 这些对象保存了每个对话的状态,因此当用户再次发送消息时,机器人可以从中断处继续。

Assumptions: 假设:

  • You're using the outgoing webhooks from slack, not the Real Time Messaging API 您正在使用松弛的传出webhook,而不是Real Time Messaging API
  • You're not trying to do some kind of multiple question-answer response where state between each question & answer needs to be maintained. 你不是试图做一些多问答案的回答,其中每个问题和答案之间的状态需要保持。

Skip all the Django stuff and just use AWS Lambda to respond to user requests. 跳过所有Django的东西,只需使用AWS Lambda来响应用户请求。 That only works for fairly simple "MyBot: do_something_for_me" style things but it's working pretty well for us. 这仅适用于相当简单的“MyBot:do_something_for_me”风格的东西,但它对我们来说效果很好。 Lot easier to manage as well since there's no ec2, no rds, easy deployment, etc. Just make sure you set a reasonable time limit for each Lambda request. 因为没有ec2,没有rds,易于部署等等,因此更容易管理。只需确保为每个Lambda请求设置合理的时间限制。 From my experience 3 seconds is generally enough time unless you've got a bit of a larger script. 根据我的经验,3秒通常是足够的时间,除非你有一个更大的脚本。

If you really really really have to maintain all this state then you might be better off just writing some kind of quick thing in Flask rather than going through all the setup of django. 如果你真的真的必须保持所有这些状态那么你可能最好只在Flask写一些快速的东西而不是经历django的所有设置。 You'll then have to deal with all the deployment, autoscaling, backup rigmarole that you would for any web service but if you need it, well then ya need it =) 然后,您将不得不处理所有部署,自动扩展,备份操作以及任何Web服务,但如果您需要它,那么您需要它=)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM