过去几天,我一直在为此苦苦挣扎,但是...当我从slack应用程序使用我的命令调用Slack Slash Commands时,Slack不会以任何有关该命令的数据作为响应,例如trigger_id。我想通过调用dialog.open来响应命令,但这需要命令中的trigger_id。
当前,我正在记录服务器的响应;打印出以下内容(我将其托管在Heroku中,因此这四个数字与一个时间戳有关,我无法轻松地从日志中完全删除该时间戳,只是忽略它们。我也从一开始就省略了很大一部分)由于StackOverflow的字符数限制而产生的响应):
1699 length: 0,
1700 pipes: null,
1701 pipesCount: 0,
1703 flowing: true,
1704 ended: false,
1706 endEmitted: false,
1707 reading: true,
1709 sync: false,
1710 needReadable: true,
1711 emittedReadable: false,
1713 readableListening: false,
1714 resumeScheduled: false,
1716 paused: false,
1718 emitClose: false,
1719 destroyed: false,
1721 defaultEncoding: 'utf8',
1722 awaitDrain: 0,
1724 readingMore: false,
1725 decoder: null,
1727 encoding: null },
1728 readable: true,
1730 _events:
1731 [Object: null prototype] {
1732 end: [Array],
1734 drain: [Array],
1735 timeout: [Function: socketOnTimeout],
1737 data: [Function: bound socketOnData],
1738 error: [Function: socketOnError],
1739 close: [Array],
1741 resume: [Function: onSocketResume],
1742 pause: [Function: onSocketPause] },
1744 _eventsCount: 8,
1745 _maxListeners: undefined,
1747 _writableState:
1748 WritableState {
1750 objectMode: false,
1751 highWaterMark: 16384,
1753 finalCalled: false,
1754 needDrain: false,
1755 ending: false,
1757 ended: false,
1758 finished: false,
1760 destroyed: false,
1761 decodeStrings: false,
1762 defaultEncoding: 'utf8',
1764 length: 0,
1765 writing: false,
1767 corked: 0,
1768 sync: true,
1769 bufferProcessing: false,
1771 onwrite: [Function: bound onwrite],
1772 writecb: null,
1774 writelen: 0,
1775 bufferedRequest: null,
1777 lastBufferedRequest: null,
1778 pendingcb: 0,
1779 prefinished: false,
1781 errorEmitted: false,
1782 emitClose: false,
1783 bufferedRequestCount: 0,
1785 corkedRequestsFree: [Object] },
1786 writable: true,
1788 allowHalfOpen: true,
1789 _sockname: null,
1790 _pendingData: null,
1792 _pendingEncoding: '',
1793 server:
1800 Server {
1802 _events: [Object],
1803 _eventsCount: 2,
1804 _maxListeners: undefined,
1806 _connections: 1,
1807 _handle: [TCP],
1809 _usingWorkers: false,
1810 _workers: [],
1812 _unref: false,
1813 allowHalfOpen: true,
1814 pauseOnConnect: false,
1816 httpAllowHalfOpen: false,
1817 timeout: 120000,
1818 keepAliveTimeout: 5000,
1820 _pendingResponseData: 0,
1821 maxHeadersCount: null,
1822 headersTimeout: 40000,
1824 _connectionKey: '6::::33915',
1825 [Symbol(IncomingMessage)]: [Function],
1826 [Symbol(ServerResponse)]: [Function],
1828 [Symbol(asyncId)]: 4 },
1829 _server:
1831 Server {
1832 _events: [Object],
1833 _eventsCount: 2,
1835 _maxListeners: undefined,
1836 _connections: 1,
1838 _handle: [TCP],
1839 _usingWorkers: false,
1840 _workers: [],
1842 _unref: false,
1844 allowHalfOpen: true,
1845 pauseOnConnect: false,
1847 httpAllowHalfOpen: false,
1848 timeout: 120000,
1850 keepAliveTimeout: 5000,
1851 _pendingResponseData: 0,
1852 maxHeadersCount: null,
1854 headersTimeout: 40000,
1855 _connectionKey: '6::::33915',
1856 [Symbol(IncomingMessage)]: [Function],
1858 [Symbol(ServerResponse)]: [Function],
1859 [Symbol(asyncId)]: 4 },
1861 timeout: 120000,
1862 parser:
1864 HTTPParser {
1865 '0': [Function: parserOnHeaders],
1867 '1': [Function: parserOnHeadersComplete],
1868 '2': [Function: parserOnBody],
1870 '3': [Function: parserOnMessageComplete],
1871 '4': [Function: bound onParserExecute],
1873 _headers: [],
1874 _url: '',
1875 socket: [Circular],
1877 incoming: [Circular],
1878 outgoing: null,
1880 maxHeaderPairs: 2000,
1881 _consumed: true,
1883 onIncoming: [Function: bound parserOnIncoming],
1884 parsingHeadersStart: 0,
1886 [Symbol(isReused)]: true },
1887 on: [Function: socketOnWrap],
1888 _paused: false,
1890 _httpMessage:
1891 ServerResponse {
1893 _events: [Object],
1894 _eventsCount: 1,
1896 _maxListeners: undefined,
1897 output: [],
1898 outputEncodings: [],
1900 outputCallbacks: [],
1901 outputSize: 0,
1903 writable: true,
1904 _last: false,
1905 chunkedEncoding: false,
1907 shouldKeepAlive: false,
1908 useChunkedEncodingByDefault: true,
1910 sendDate: true,
1911 _removedConnection: false,
1912 _removedContLen: false,
1914 _removedTE: false,
1915 _contentLength: null,
1916 _hasBody: true,
1918 _trailer: '',
1919 finished: false,
1921 _headerSent: false,
1922 socket: [Circular],
1924 connection: [Circular],
1925 _header: null,
1926 _onPendingData: [Function: bound updateOutgoingData],
1928 _sent100: false,
1933 _expect_continue: false,
1934 req: [Circular],
1936 locals: [Object: null prototype] {},
1937 [Symbol(isCorked)]: false,
1938 [Symbol(outHeadersKey)]: [Object] },
1940 [Symbol(asyncId)]: 178,
1941 [Symbol(lastWriteQueueSize)]: 0,
1943 [Symbol(timeout)]:
1944 Timeout {
1945 _called: false,
1947 _idleTimeout: 120000,
1948 _idlePrev: [TimersList],
1950 _idleNext: [TimersList],
1951 _idleStart: 1148319,
1952 _onTimeout: [Function: bound ],
1954 _timerArgs: undefined,
1955 _repeat: null,
1957 _destroyed: false,
1958 [Symbol(unrefed)]: true,
1960 [Symbol(asyncId)]: 179,
1961 [Symbol(triggerId)]: 178 },
1962 [Symbol(kBytesRead)]: 0,
1964 [Symbol(kBytesWritten)]: 0 },
1965 _consuming: false,
1967 _dumped: false,
1968 next: [Function: next],
1970 baseUrl: '',
1971 originalUrl: '/create',
1973 _parsedUrl:
1974 Url {
1975 protocol: null,
1977 slashes: null,
1978 auth: null,
1979 host: null,
1981 port: null,
1982 hostname: null,
1984 hash: null,
1985 search: null,
1988 query: null,
1989 pathname: '/create',
1991 path: '/create',
1992 href: '/create',
1994 _raw: '/create' },
1995 params: {},
1996 query: {},
1998 res:
1999 ServerResponse {
2001 _events:
2002 [Object: null prototype] { finish: [Function: bound resOnFinish] },
2003 _eventsCount: 1,
2005 _maxListeners: undefined,
2011 output: [],
2012 outputEncodings: [],
2014 outputCallbacks: [],
2015 outputSize: 0,
2017 writable: true,
2018 _last: false,
2019 chunkedEncoding: false,
2021 shouldKeepAlive: false,
2022 useChunkedEncodingByDefault: true,
2024 sendDate: true,
2025 _removedConnection: false,
2027 _removedContLen: false,
2028 _removedTE: false,
2029 _contentLength: null,
2031 _hasBody: true,
2032 _trailer: '',
2034 finished: false,
2035 _headerSent: false,
2036 socket:
2038 Socket {
2039 connecting: false,
2041 _hadError: false,
2042 _handle: [TCP],
2044 _parent: null,
2045 _host: null,
2046 _readableState: [ReadableState],
2048 readable: true,
2049 _events: [Object],
2051 _eventsCount: 8,
2052 _maxListeners: undefined,
2053 _writableState: [WritableState],
2055 writable: true,
2056 allowHalfOpen: true,
2058 _sockname: null,
2059 _pendingData: null,
2061 _pendingEncoding: '',
2062 server: [Server],
2064 _server: [Server],
2065 timeout: 120000,
2066 parser: [HTTPParser],
2068 on: [Function: socketOnWrap],
2069 _paused: false,
2071 _httpMessage: [Circular],
2072 [Symbol(asyncId)]: 178,
2074 [Symbol(lastWriteQueueSize)]: 0,
2075 [Symbol(timeout)]:
2076 Timeout {
2078 _called: false,
2079 _idleTimeout: 120000,
2080 _idlePrev: [TimersList],
2083 _idleNext: [TimersList],
2084 _idleStart: 1148319,
2086 _onTimeout: [Function: bound ],
2087 _timerArgs: undefined,
2089 _repeat: null,
2090 _destroyed: false,
2091 [Symbol(unrefed)]: true,
2093 [Symbol(asyncId)]: 179,
2094 [Symbol(triggerId)]: 178 },
2095 [Symbol(kBytesRead)]: 0,
2097 [Symbol(kBytesWritten)]: 0 },
2098 connection:
2100 Socket {
2101 connecting: false,
2103 _hadError: false,
2104 _handle: [TCP],
2105 _parent: null,
2107 _host: null,
2108 _readableState: [ReadableState],
2109 readable: true,
2111 _events: [Object],
2112 _eventsCount: 8,
2114 _maxListeners: undefined,
2115 _writableState: [WritableState],
2116 writable: true,
2118 allowHalfOpen: true,
2119 _sockname: null,
2121 _pendingData: null,
2122 _pendingEncoding: '',
2123 server: [Server],
2125 _server: [Server],
2126 timeout: 120000,
2128 parser: [HTTPParser],
2129 on: [Function: socketOnWrap],
2131 _paused: false,
2132 _httpMessage: [Circular],
2133 [Symbol(asyncId)]: 178,
2135 [Symbol(lastWriteQueueSize)]: 0,
2136 [Symbol(timeout)]:
2138 Timeout {
2139 _called: false,
2140 _idleTimeout: 120000,
2142 _idlePrev: [TimersList],
2143 _idleNext: [TimersList],
2145 _idleStart: 1148319,
2146 _onTimeout: [Function: bound ],
2148 _timerArgs: undefined,
2149 _repeat: null,
2151 _destroyed: false,
2152 [Symbol(unrefed)]: true,
2153 [Symbol(asyncId)]: 179,
2155 [Symbol(triggerId)]: 178 },
2156 [Symbol(kBytesRead)]: 0,
2158 [Symbol(kBytesWritten)]: 0 },
2160 _header: null,
2161 _onPendingData: [Function: bound updateOutgoingData],
2163 _sent100: false,
2164 _expect_continue: false,
2166 req: [Circular],
2167 locals: [Object: null prototype] {},
2169 [Symbol(isCorked)]: false,
2171 [Symbol(outHeadersKey)]: [Object: null prototype] { 'x-powered-by': [Array] } },
2172 route:
2174 Route {
2175 path: '/create',
2176 stack: [ [Layer], [Layer] ],
2178 methods: { post: true } },
2179 body: {} }
根据我看过的所有文档,示例和教程,我应该从请求的主体中获取trigger_id,并且我认为我应该能够从响应参数中获取数据。但是,请求中的参数,查询和正文都是空的。
示例:https://github.com/slackapi/template-slash-command-and-dialogs
作为旁注:我没有明确遵循本教程或使用示例;我指的是他们做了什么,以找出我做错了什么。
1995 params: {},
1996 query: {}
和
2179 body: {}
那么应该在正文,查询或参数中包含数据吗?我相信从网上看到的内容都是如此,但这是我第一次尝试使用Slack僵尸程序,至少从我所看到的内容来看,Slack没有最好的文档,因此我以某种方式将其称为错误是很合理的吗? ?
更新,这是我在服务器上调用的路由:
app.post('/create', jsonParser, function(req, res) {
console.log(req);
var {
text, trigger_id
} = req.body;
// if (reqBody.token != botToken) {
// res.status(403).end("Access forbidden")
// } else {
res.status(200).end() // best practice to respond with empty 200 status code
//Sample Dialog response from the GitHub example, to test if route is working
const dialog = {
token: botToken,
trigger_id: trigger_id,
dialog: JSON.stringify({
title: 'Submit a helpdesk ticket',
callback_id: 'submit-ticket',
submit_label: 'Submit',
elements: [{
label: 'Title',
type: 'text',
name: 'title',
value: text,
hint: '30 second summary of the problem',
}, {
label: 'Description',
type: 'textarea',
name: 'description',
optional: true,
}, {
label: 'Urgency',
type: 'select',
name: 'urgency',
options: [{
label: 'Low',
value: 'Low'
}, {
label: 'Medium',
value: 'Medium'
}, {
label: 'High',
value: 'High'
}, ],
}, ],
}),
};
// open the dialog by calling dialogs.open method and sending the payload
bot.dialog.open({
dialog
}).then(console.log)
});
答案 0 :(得分:1)
您的身体为空的原因是您当前的代码假定来自Slack的请求按JSON编码发送。而是将其编码为标准POST请求(application/x-www-form-urlencoded
)。
替换
app.post('/create', jsonParser, function(req, res) {
使用
app.use(bodyParser.urlencoded({ extended: true }))
可以解决问题。