简体   繁体   English

运行 python 函数的 Slack 机器人

[英]Slack bot running python functions

I am creating a Slack Bot, I have successfully installed it, although I am not 100% sure what does ngrok do to run it when I have node server running locally.我正在创建一个 Slack Bot,我已经成功安装了它,尽管我不能 100% 确定当我在本地运行节点服务器时 ngrok 会做什么来运行它。 I have followed this guide .遵循了本指南

I have a python script that:我有一个 python 脚本:

  1. Connects to a database连接到数据库
  2. Downloads the entire table and creates a pandas dataframe下载整个表并创建一个熊猫数据框

In the script, I have declared many variables which takes specific information from the dataframe.在脚本中,我声明了许多从数据框中获取特定信息的变量。

For example:例如:

monday_sales = sum((df['sale_event_time']>=week) & (df['sale_event_time']<=today_real) & ((df['sale_event_time'].apply(lambda x:x.isoweekday()))==1))

Which simply returns how many sales I had on Monday from the dataframe.这只是从数据框中返回我周一的销售额。

My goal is to connect slack bot to my python script, basically being able to run /monday_sales in slack which would print the number that monday_sales variable holds.我的目标是将 slack bot 连接到我的 python 脚本,基本上能够在 slack 中运行/monday_sales这将打印monday_sales变量保存的数字。

I haven't used much of node or js, so linking everything together I find a bit difficult.我没有使用过多的 node 或 js,因此将所有内容链接在一起我觉得有点困难。

First I run ngrok like so:首先,我像这样运行 ngrok:

./ngrok http 4390

Then I run node server locally(?)然后我在本地运行节点服务器(?)

var http = require('http');
const PORT=4390;
function handleRequest(request, response){
  response.end('My bot is ALIIIIIIVEEEEE');
}
var server = http.createServer(handleRequest);
server.listen(PORT, function(){
  console.log("Server listening on: http://localhost:%s", PORT);
});

And when I run /ngrok in slack it prints out 'My bot is ALIIIIIIVEEEEE, although only visible to me.当我在 slack 中运行/ngrok ,它会打印出“我的机器人是 ALIIIIIIIVEEEEE,尽管只有我才能看到。

So the question could be split in to few:所以这个问题可以分为几个:

  1. Why do I need ngrok and node.js server running locally?为什么我需要在本地运行 ngrok 和 node.js 服务器?
  2. How can I trigger python functions / variables from slack bot.如何从 slack bot 触发 python 函数/变量。
  3. Is there a better way of having slack bot only ran by python without node, ngrok?有没有更好的方法让 slack bot 只由 python 运行而没有节点,ngrok?

Thank you very much for your suggestions.非常感谢您的建议。

I think your best bet is to ditch this tutorial and Node, as it seems you're only using it as a ngrok wrapper.我认为最好的办法是ngrok本教程和 Node,因为您似乎只是将它用作ngrok包装器。 Instead, since the rest of your bot is in Python, just use pyngrok (full disclosure, I am the developer), which is a Python wrapper for ngrok .相反,由于机器人的其余部分使用 Python,只需使用pyngrok (完全公开,我是开发人员),它是ngrok的 Python 包装器。 It gives you programmatic access to the tunnels and everything else.它使您能够以编程方式访问隧道和其他所有内容。

If this solves your use case, you can check out the docs here .如果这解决了您的用例,您可以在此处查看文档。

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

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