下面是我的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
我创建了带有/my-command
选项的松弛斜杠命令POST
,并粘贴了ngrok提供的https URL:
现在,当我在空闲中运行/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有一个过期时间。如何扩展这个过期时间
或者在过期的情况下,是否有其他方式向用户发送关于测试完成的消息?我有一个启动/my-command
的用户的名字和id
req.PostFormValue("user_name")
req.PostFormValue("user_id")
因此,我想运行集成测试的松弛,这是超过2个小时,并得到一个响应后,完成这样的测试在松弛。
发布于 2016-10-03 08:36:37
不能增加URL的过期时间,该URL是Slack内部设置。
使用
您可以通过需要令牌的松弛Web API向任何用户发送无人值守的消息。
有关更多信息,请查看:https://api.slack.com/web。
chat.postMessage
Slack有一个postMessage
命令,允许用户通过IM
通道(直接消息)向信道、slackbot
通道和用户发送消息。似乎你想做后一件事,这很简单。
发送到IM频道
方法Url:https://slack.com/api/chat.postMessage
当根据channel
的值设置as_user
的值时,将消息发送到IM通道会稍微复杂一些。
as_user
是假的:@chris
)作为channel
的值传递给该用户的@slackbot通道作为bot。D023BB3L2
)作为channel
的值传递给该IM通道作为bot。可以通过im.list API方法检索IM通道的ID。
as_user
是真的:D023BB3L2
)作为channel
的值传递给该IM通道,作为经过身份验证的用户。可以通过im.list API方法检索IM通道的ID。若要向拥有在请求中使用的令牌的用户发送直接消息,请向通道字段提供在im.list等方法中找到的会话/IM ID值。
若要向拥有请求中使用的令牌的用户发送直接消息,请向channel
字段提供在im.list之类的方法中找到的会话/ ID值。
im.list
如果用户没有打开通道,但可以调用im.open
,则此方法将返回直接消息通道的列表。
im.open
此方法用于与指定用户的直接消息open
channel
。
有关im.open
的文档可以找到这里。
示例URL
https://slack.com/api/chat.postMessage?token=**TOKEN**&channel=**Direct Channel ID**&text=HelloWorld&as_user=true&pretty=1
只需将**TOKEN**
和**Direct Channel ID**
替换为您的值,它将向指定的用户发送直接消息。
https://stackoverflow.com/questions/39788408
复制相似问题