首页 文章

slack integration:404 Expired url https://hooks.slack.com/commands

提问于
浏览
2

这是来自slack命令的golang应用程序侦听请求: 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使其可以从互联网访问:

ngrok http 8383

我用 POST 选项创建了slack slash命令 /my-command 并粘贴了ngrok给出的https URL:

slash command options - slack

现在,当我在松弛中运行 /my-command 时,我得到了松弛的回复 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有一个到期时间 . 如何 extendexpiration time

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

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

因此,我希望通过松弛运行积分测试,这些测试超过2小时,并在完成此类测试后获得响应 .

1 回答

  • 2

    您无法增加作为Slack内部设置的URL的过期时间 .

    使用Slack Web API

    您可以通过Slack Web API向任何用户发送无人参与的消息,您需要为其提供令牌 .

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

    chat.postMessage

    Slack API有一个 postMessage 命令,允许用户通过 IM channel(直接消息)将消息发布到通道, slackbot 通道和用户 . 看来你想要做的很晚,这很简单 .

    发布到IM Channels

    chat.postMessage#channels

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

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

    • 如果 as_user 为假:

    • 将用户名( @chris )作为 channel 的值传递给该用户的@slackbot Channels 作为僵尸程序 .

    • 将IM通道的ID( D023BB3L2 )作为 channel 的值传递给作为机器人的IM通道 . 可以通过im.list API方法检索IM通道的ID .

    • 如果 as_user 为真:

    • 将IM通道的ID( D023BB3L2 )作为 channel 的值传递,以作为经过身份验证的用户发布到该IM通道 . 可以通过im.list API方法检索IM通道的ID . 要向拥有请求中使用的令牌的用户发送直接消息,请为 Channels 字段提供在im.list等方法中找到的会话/ IM ID值 .

    要向请求中使用的令牌发送直接消息 owning ,请在 channel 字段中提供在im.list等方法中找到的会话/ IM ID值 .

    im.list

    如果您没有与用户打开 Channels ,此方法将返回直接消息 Channels 列表,但您可以调用 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** 替换为您的值,它应该向指定用户发送直接消息 .

相关问题