Skip to content

Commit 2ada2a1

Browse files
committed
[Update] Example7: Add slack webhook example
1 parent 4e5df3a commit 2ada2a1

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

components/Example7.vue

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<script>
2+
export default {
3+
name: 'Example7',
4+
data() {
5+
return {
6+
form: {
7+
name: ''
8+
},
9+
response: '',
10+
error: null
11+
}
12+
},
13+
methods: {
14+
async sendSlackMessage(name) {
15+
try {
16+
const res = await this.$axios.$post(
17+
'/.netlify/functions/slack-webhook',
18+
{ name: name }
19+
)
20+
this.response = res
21+
this.error = null
22+
} catch (e) {
23+
this.error = e.response
24+
this.response = ''
25+
}
26+
}
27+
}
28+
}
29+
</script>
30+
31+
<template>
32+
<ElForm ref="form" :model="form" inline label-width="auto" label-position="left" @submit.native.prevent="sendSlackMessage(form.name)">
33+
<h2>7. Send a Slack message</h2>
34+
<ElFormItem label="Name">
35+
<ElInput v-model="form.name" placeholder="Your name" />
36+
</ElFormItem>
37+
<ElButton type="primary" @click="sendSlackMessage(form.name)">👋 Say hello</ElButton>
38+
<p>Response: {{ response }}</p>
39+
<p v-if="error" style="color:red;"><strong>Error {{ error.status }}</strong><br>{{ error.data }}</p>
40+
</ElForm>
41+
</template>

netlify-lambda-src/slack-webhook.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import fetch from 'node-fetch'
2+
require('dotenv').config()
3+
4+
// eslint-disable-next-line require-await
5+
exports.handler = async (event, context) => {
6+
// Only allow POST
7+
if (event.httpMethod !== 'POST') {
8+
return { statusCode: 405, body: 'Method Not Allowed' }
9+
}
10+
11+
const data = JSON.parse(event.body)
12+
const name = data.name || 'noname'
13+
14+
// Send greeting to Slack
15+
return fetch(process.env.SLACK_WEBHOOK_URL, {
16+
headers: {
17+
'content-type': 'application/json'
18+
},
19+
method: 'POST',
20+
body: JSON.stringify({ text: `${name} says hello!` })
21+
})
22+
.then(() => ({
23+
statusCode: 200,
24+
body: `Hello, ${name}! Your greeting has been sent to Slack 👋`
25+
}))
26+
.catch(error => ({
27+
statusCode: 422,
28+
body: `Oops! Something went wrong. ${error}`
29+
}))
30+
}

pages/index.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Example3 from '@/components/Example3'
55
import Example4 from '@/components/Example4'
66
import Example5 from '@/components/Example5'
77
import Example6 from '@/components/Example6'
8+
import Example7 from '@/components/Example7'
89
910
export default {
1011
components: {
@@ -13,7 +14,8 @@ export default {
1314
Example3,
1415
Example4,
1516
Example5,
16-
Example6
17+
Example6,
18+
Example7
1719
}
1820
}
1921
</script>
@@ -33,6 +35,8 @@ export default {
3335
<hr class="separator">
3436
<Example6 />
3537
<hr class="separator">
38+
<Example7 />
39+
<hr class="separator">
3640
</div>
3741
</template>
3842

0 commit comments

Comments
 (0)