【问题标题】:slack integration: 404 Expired url https://hooks.slack.com/commands松弛集成:404 过期网址 https://hooks.slack.com/commands
【发布时间】:2016-10-03 16:36:37
【问题描述】:

这是我的 golang 应用程序监听来自 slack 命令的请求: main.go:

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "time"
)

type SlackCmdResponse struct {
    Text string `json:"text"`
}

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
        if err := req.ParseForm(); err != nil {
            panic(err)
        }
        responseUrl := req.PostFormValue("response_url")
        go func() {
            time.Sleep(time.Second)
            postBack(responseUrl)
        }()
        rj, err := json.Marshal(SlackCmdResponse{Text: "Test started"})
        if err != nil {
            panic(err)
        }
        w.Header().Set("Content-Type", "application/json")
        w.Write(rj)

    })
    fmt.Println("listening 8383")
    if err := http.ListenAndServe(":8383", nil); err != nil {
        panic(err)
    }
}

func postBack(responseUrl string) {
    fmt.Println("responseUrl", responseUrl)
    cResp := SlackCmdResponse{
        Text: "Test finished",
    }
    cj, err := json.Marshal(cResp)
    if err != nil {
        panic(err)
    }
    req, err := http.NewRequest("POST", responseUrl, bytes.NewReader(cj))
    req.Header.Set("Content-Type", "application/json")

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        panic(err)
    }
    if resp != nil {
        fmt.Println(resp.StatusCode)
        if b, err := ioutil.ReadAll(resp.Body); err != nil {
            panic(err)
        } else {
            fmt.Println(string(b))
        }
        if resp.Body != nil {
            resp.Body.Close()
        }
    }
}

我运行它:

$ go run main.go
listening 8383

我使用 ngrok 使其可以从 Internet 访问:

ngrok http 8383

我使用POST 选项创建了斜杠命令/my-command 并粘贴了ngrok 提供的https URL:

现在,当我在 slack 中运行 /my-command 时,我得到了 slack 回复 Test started。然后在第二次松弛打印Test finished

目前,一切都很好。

问题

如果我换行

time.Sleep(time.Second)

逐行

time.Sleep(time.Hour) // long test

我不会在一小时内松弛打印“测试完成”。相反,我在我的应用日志中看到:

responseUrl https://hooks.slack.com/commands/T3GBFUZ64/86817661808/XRmDO21jYaP1Wzu7GFpNw9eW
404
Expired url

看起来 slack 的响应 URL 有一个过期时间。如何延长这个过期时间

或者在过期的情况下,是否有另一种方法可以向用户发送有关测试已完成的消息?我有启动/my-command的用户的姓名和ID

req.PostFormValue("user_name")
req.PostFormValue("user_id")

所以我想通过 slack 运行超过 2 小时的集成测试,并在 slack 中完成此类测试后得到响应。

【问题讨论】:

  • 我对此一无所知(因为我是编程的初学者),但我喜欢学习这样的东西,你能说这是什么,希望这个链接对你有所帮助你,如果不对不起我:(gist.github.com/jmervine/8321794祝你好运@Maxim :)

标签: go slack


【解决方案1】:

您不能增加作为 Slack 内部设置的 URL 的过期时间。

使用 Slack Web API

您可以通过Slack Web API 向任何用户发送无人参与的消息,您需要一个令牌。

更多信息请查看:https://api.slack.com/web

chat.postMessage

Slack API 有一个postMessage 命令,允许用户将消息发布到频道、slackbot 频道以及通过IM 频道(直接消息)向用户发布。看来你想做后者,这很简单。

发布到 IM 频道

chat.postMessage#channels

方法网址:https://slack.com/api/chat.postMessage

根据as_user 的值设置channel 的值时,发布到IM 频道会稍微复杂一些。

  • 如果as_user 为假:
    • 传递用户名 (@chris) 作为 channel 的值,以作为机器人发布到该用户的 @slackbot 频道。
    • 传递 IM 频道的 ID (D023BB3L2) 作为 channel 的值,以作为机器人发布到该 IM 频道。 IM频道ID可以通过im.list API方法获取。
  • 如果as_user 为真:
    • 传递 IM 频道的 ID (D023BB3L2) 作为 channel 的值,以作为经过身份验证的用户发布到该 IM 频道。可以通过 im.list API 方法检索 IM 频道的 ID。 要向拥有请求中使用的令牌的用户发送直接消息,请向频道字段提供对话/IM ID 值,该值可在 im.list 等方法中找到。

要向拥有请求中使用的令牌的用户发送直接消息,请向channel 字段提供在像im.list这样的方法。

im.list

如果您没有与用户打开频道但您可以调用im.open,此方法将返回直接消息频道列表。

im.open

此方法用于open与指定用户直接消息channel

关于im.open的文档可以在here找到。

示例网址

https://slack.com/api/chat.postMessage?token=**TOKEN**&channel=**Direct Channel ID**&text=HelloWorld&as_user=true&pretty=1

只需将**TOKEN****Direct Channel ID** 替换为您的值,它就会向指定用户发送直接消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 2018-04-25
    • 2020-11-24
    • 1970-01-01
    相关资源
    最近更新 更多