File tree Expand file tree Collapse file tree 3 files changed +76
-1
lines changed Expand file tree Collapse file tree 3 files changed +76
-1
lines changed Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import Example3 from '@/components/Example3'
5
5
import Example4 from ' @/components/Example4'
6
6
import Example5 from ' @/components/Example5'
7
7
import Example6 from ' @/components/Example6'
8
+ import Example7 from ' @/components/Example7'
8
9
9
10
export default {
10
11
components: {
@@ -13,7 +14,8 @@ export default {
13
14
Example3,
14
15
Example4,
15
16
Example5,
16
- Example6
17
+ Example6,
18
+ Example7
17
19
}
18
20
}
19
21
</script >
@@ -33,6 +35,8 @@ export default {
33
35
<hr class =" separator" >
34
36
<Example6 />
35
37
<hr class =" separator" >
38
+ <Example7 />
39
+ <hr class =" separator" >
36
40
</div >
37
41
</template >
38
42
You can’t perform that action at this time.
0 commit comments