-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
56 lines (46 loc) · 1.91 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const { App, LogLevel } = require('@slack/bolt');
require('dotenv').config();
//importing interface and api-function from weather-api.ts
import { WeatherData, getWeatherData } from "./weather-api";
// Create a new Slack Bolt app
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
socketMode: true,
logLevel: LogLevel.INFO,
signingSecret: process.env.SLACK_SIGNING_SECRET,
appToken : process.env.SLACK_APP_TOKEN,
});
// Listen for the /weather command and respond with the current weather data for the specified city
app.command('/weather', async ({ ack, command, respond ,client} : {ack : Function , command : any , client : any , respond : any}): Promise<void> => {
// Acknowledge the command request
await ack();
// Get the city from the command text
const city: string = command.text;
try {
// Get the current weather data for the specified city
const weatherData = await getWeatherData(city);
// Extract the temperature, description, and humidity from the weather data
const temperature: number = weatherData.main.temp;
const description: string = weatherData.weather[0].description;
const humidity: number = weatherData.main.humidity;
// Message to send back to the user
const message: string = `The current temperature and humidity in ${city} is ${temperature}°C and ${humidity} respectively with ${description}.`;
// Post the message in the channel where the command was issued
await client.respond({
response_type: 'in_channel',
text: message,
});
} catch (error) {
console.error(error);
// Post an error message in the channel where the command was issued
await client.respond({
response_type: 'ephemeral',
text: `Sorry, there was an error retrieving the weather data for ${city}.`,
});
}
});
// Start the app
(async (): Promise<void> => {
await app.start(process.env.PORT || 3000);
console.log('Eureka!');
})();