【发布时间】:2020-05-21 07:16:59
【问题描述】:
我正在使用 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 docs 正在显示图像的 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 !!
【问题讨论】:
标签: node.js json axios bots slack-api