我正在使用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}* °C\n\n>_Humidité_ : *${humidity}* %`)
});
}如果有人已经使用slack- bot -api并在他的机器人上发布了图片,我想知道你是怎么做到的,因为我的想法已经用完了,slack 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 Kalkoken!
发布于 2020-02-05 21:07:33
这个库只是将参数转发给相应的Slack API方法,即chat.postMessage。
要使用Slack发布图像,您需要在attachment (过时的)或blocks中包含图像URL。
例如,要附加带有附件的图像,您需要通过params添加此阵列
{
"attachments":
[
{
"fallback": "this did not work",
"image_url": "https://a.slack-edge.com/80588/img/blocks/bkb_template_images/beagle.png"
}
]
}https://stackoverflow.com/questions/60074708
复制相似问题