无法使用 Slackbots 使用 Slackbot 发送图像 (javascript)



我正在使用slackbots(https://github.com/mishk0/slack-bot-api(创建一个交互式机器人。 作为用户,我可以在聊天中发送消息,并让我的机器人根据我写的内容给我答案。就像我输入 : !weekend,我的机器人将通过获取 API 来回答周末是否接近。

它以这种方式工作

bot.on('message', data => {
if (data.type !== 'message') {
return;
}
handleMessage(data.text);
});
function handleMessage(message) {
switch (message) {
case '!weekend':
case '!we':
fetchWeekendData();
break;
case '!apero':
case '!biere':
case '!pastis':
fetchAperoData();
break;
case '!meteo':
fetchWeatherData();
break;
case '!metro':
fetchMetroData('A');
fetchMetroData('R');
break;
case '!features':
getFeatures();
break;
}
}
function fetchWeekendData() {
axios.get('https://estcequecestbientotleweekend.fr/api',
).then(res => {
const weText = res.data.text;
postToChannel(`_Bientôt le week end ?_ : *${weText}*`);
});
}
function postToChannel(message) {
bot.postMessageToChannel(
'général',
message,
params
)
}

我有另一个函数调用,这次是一个天气 api。API 响应了许多信息和 根据天气情况指向 PNG 的链接。一个小天气图标。

问题是,当我将消息发回聊天时,它会发送图像链接,我只是无法弄清楚如何在聊天中将此链接作为图像获取。

function fetchWeatherData() {
axios.get('http://api.weatherstack.com/current?access_key=myAPIkey&query=Paris&units=m',
).then(res => {
const location = res.data.location.name;
const icon = res.data.current.weather_icons;
const temperature = res.data.current.temperature;
const feelslike = res.data.current.feelslike;
const humidity = res.data.current.humidity;
postToChannel(`Météo à *${location}* : ${icon} n>_Température_ : *${temperature}* °C _Ressenti_ : *${feelslike}* °Cnn>_Humidité_ : *${humidity}* %`)
});
}

如果有人已经使用过 slack-bot-api 并使用他的机器人发布图像,我想知道你是怎么做到的,因为我没有主意了,松弛 api 文档显示图像的 JSON 附件,但我不知道如何用这个包使用它。

编辑:好的,它解决了,这样

function postToChannelWithImage() {
bot.postMessageToChannel(
'général',
'yikes itis working',
params = {
"icon_url": "https://avatars.slack-edge.com/2020-02-04/935676215584_38623245747b942874b5_192.jpg",
"attachments":
[
{
"fallback": "this did not work",
"image_url": "https://a.slack-edge.com/80588/img/blocks/bkb_template_images/beagle.png"
}
]
}
)
}

我添加了数组并且它运行良好,我只需要将其他参数添加到同一个数组中即可保持我的配置原样。

非常感谢@Erik卡尔科肯!

这个库接缝只是将参数转发到相应的 Slack API 方法,这是chat.postMessage.

要使用 Slack 发布图像,您需要在附件(过时(或块中包含图像 URL。

例如,要附加带有附件的图像,您将通过params添加此数组:

{
"attachments":
[
{
"fallback": "this did not work",
"image_url": "https://a.slack-edge.com/80588/img/blocks/bkb_template_images/beagle.png"
}       
]
}

最新更新