-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndex.js
More file actions
275 lines (222 loc) · 11.3 KB
/
Copy pathIndex.js
File metadata and controls
275 lines (222 loc) · 11.3 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
Platform Messenger
Pendahuluan
Memulai
Penyiapan Webhook
Penyiapan Aplikasi
Panduan Memulai
Kit Desain Platform
Bot Sampel
Pengiriman Pesan
Webhook
Webview
Penemuan & Interaksi Kembali
ID & Profil
Pemrosesan Bahasa Alami (Natural Language Processing)
Analytics & Feedback
Ajukan Bot Anda!
Pedoman Kebijakan & Penggunaan
Referensi
Sumber Daya Bermanfaat
PERTANYAAN UMUM
Catatan Perubahan
Di Halaman Ini
Menyiapkan Webhook Anda
Webhook Anda adalah inti dari pengalaman Messenger Anda. Di sinilah kode Anda berada, dan di sinilah Anda akan menerima, memproses, dan mengirimkan pesan.
Di dalam panduan ini, Anda akan mempelajari cara menyiapkan webhook sederhana yang mendukung langkah-langkah verifikasi webhook yang diwajibkan Platform Messenger, serta cara menerima peristiwa webhook.
Untuk informasi selengkapnya tentang persyaratan dan peristiwa webhook, lihat Webhook.
Persyaratan
Agar dapat mengikuti panduan penyiapan webhook Anda, Anda hanya perlu memiliki komputer yang menginstal Node.js.
Untuk menjalankan webhook yang dapat menerima peristiwa webhook dari Platform Messenger, kode Anda harus di-hosting di server HTTPS publik yang memenuhi persyaratan berikut:
Dukungan HTTPS, Sertifikat yang ditandatangani sendiri tidak didukung
Sertifikat SSL yang valid
Port terbuka yang menerima permintaan GET dan POST
Langkah-langkah Penyiapan
Platform Messenger Bersifat Agnostik Bahasa
Untuk mengikuti panduan ini, Anda perlu menginstal Node.js, tetapi Anda dapat menulis webhook Anda dalam bahasa server yang paling Anda sukai.
Sebelum Anda memulai, pastikan server Anda memenuhi semua persyaratan yang tercantum di atas.
1Buat proyek Node.js baru
Jalankan yang berikut ini di baris perintah untuk membuat file dan dependensi yang diperlukan:
mkdir messenger-webhook // Creates a project directory cd messenger-webhook // Navigates to the new directory touch index.js // Creates empty index.js file. npm init // Creates package.json. Accept default for all questions. npm install express body-parser --save // Installs the express.js http server framework module, // and then adds them to the dependencies section of package.json file.
Jika semua berjalan lancar, maka direktori messenger-webhook Anda akan terlihat seperti ini:
index.js node_modules package.json
2Buat server HTTP
Tambahkan kode berikut ke index.js:
'use strict';
// Imports dependencies and set up http server
const
express = require('express'),
bodyParser = require('body-parser'),
app = express().use(bodyParser.json()); // creates express http server
// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'));
Platform Messenger
Pendahuluan
Memulai
Penyiapan Webhook
Penyiapan Aplikasi
Panduan Memulai
Kit Desain Platform
Bot Sampel
Pengiriman Pesan
Webhook
Webview
Penemuan & Interaksi Kembali
ID & Profil
Pemrosesan Bahasa Alami (Natural Language Processing)
Analytics & Feedback
Ajukan Bot Anda!
Pedoman Kebijakan & Penggunaan
Referensi
Sumber Daya Bermanfaat
PERTANYAAN UMUM
Catatan Perubahan
Di Halaman Ini
Menyiapkan Webhook Anda
Webhook Anda adalah inti dari pengalaman Messenger Anda. Di sinilah kode Anda berada, dan di sinilah Anda akan menerima, memproses, dan mengirimkan pesan.
Di dalam panduan ini, Anda akan mempelajari cara menyiapkan webhook sederhana yang mendukung langkah-langkah verifikasi webhook yang diwajibkan Platform Messenger, serta cara menerima peristiwa webhook.
Untuk informasi selengkapnya tentang persyaratan dan peristiwa webhook, lihat Webhook.
Persyaratan
Agar dapat mengikuti panduan penyiapan webhook Anda, Anda hanya perlu memiliki komputer yang menginstal Node.js.
Untuk menjalankan webhook yang dapat menerima peristiwa webhook dari Platform Messenger, kode Anda harus di-hosting di server HTTPS publik yang memenuhi persyaratan berikut:
Dukungan HTTPS, Sertifikat yang ditandatangani sendiri tidak didukung
Sertifikat SSL yang valid
Port terbuka yang menerima permintaan GET dan POST
Langkah-langkah Penyiapan
Platform Messenger Bersifat Agnostik Bahasa
Untuk mengikuti panduan ini, Anda perlu menginstal Node.js, tetapi Anda dapat menulis webhook Anda dalam bahasa server yang paling Anda sukai.
Sebelum Anda memulai, pastikan server Anda memenuhi semua persyaratan yang tercantum di atas.
1Buat proyek Node.js baru
Jalankan yang berikut ini di baris perintah untuk membuat file dan dependensi yang diperlukan:
mkdir messenger-webhook // Creates a project directory cd messenger-webhook // Navigates to the new directory touch index.js // Creates empty index.js file. npm init // Creates package.json. Accept default for all questions. npm install express body-parser --save // Installs the express.js http server framework module, // and then adds them to the dependencies section of package.json file.
Jika semua berjalan lancar, maka direktori messenger-webhook Anda akan terlihat seperti ini:
index.js node_modules package.json
2Buat server HTTP
Tambahkan kode berikut ke index.js:
'use strict';
// Imports dependencies and set up http server
const
express = require('express'),
bodyParser = require('body-parser'),
app = express().use(bodyParser.json()); // creates express http server
// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'));
Kode ini membuat server HTTP yang menampung permintaan di port default, maupun di port 1337 apabila tidak ada port default. Di panduan ini, kami menggunakan Express, sebuah a framework HTTP yang populer dan ringan, tetapi Anda dapat menggunakan framework apa pun yang Anda sukai untuk membuat webhook Anda.
3Tambahkan endpoint webhook Anda
Tambahkan kode berikut ke index.js:
// Creates the endpoint for our webhook
app.post('/webhook', (req, res) => {
let body = req.body;
// Checks this is an event from a page subscription
if (body.object === 'page') {
// Iterates over each entry - there may be multiple if batched
body.entry.forEach(function(entry) {
// Gets the message. entry.messaging is an array, but
// will only ever contain one message, so we get index 0
let webhook_event = entry.messaging[0];
console.log(webhook_event);
});
// Returns a '200 OK' response to all requests
res.status(200).send('EVENT_RECEIVED');
} else {
// Returns a '404 Not Found' if event is not from a page subscription
res.sendStatus(404);
}
});
Platform Messenger
Pendahuluan
Memulai
Penyiapan Webhook
Penyiapan Aplikasi
Panduan Memulai
Kit Desain Platform
Bot Sampel
Pengiriman Pesan
Webhook
Webview
Penemuan & Interaksi Kembali
ID & Profil
Pemrosesan Bahasa Alami (Natural Language Processing)
Analytics & Feedback
Ajukan Bot Anda!
Pedoman Kebijakan & Penggunaan
Referensi
Sumber Daya Bermanfaat
PERTANYAAN UMUM
Catatan Perubahan
Di Halaman Ini
Menyiapkan Webhook Anda
Webhook Anda adalah inti dari pengalaman Messenger Anda. Di sinilah kode Anda berada, dan di sinilah Anda akan menerima, memproses, dan mengirimkan pesan.
Di dalam panduan ini, Anda akan mempelajari cara menyiapkan webhook sederhana yang mendukung langkah-langkah verifikasi webhook yang diwajibkan Platform Messenger, serta cara menerima peristiwa webhook.
Untuk informasi selengkapnya tentang persyaratan dan peristiwa webhook, lihat Webhook.
Persyaratan
Agar dapat mengikuti panduan penyiapan webhook Anda, Anda hanya perlu memiliki komputer yang menginstal Node.js.
Untuk menjalankan webhook yang dapat menerima peristiwa webhook dari Platform Messenger, kode Anda harus di-hosting di server HTTPS publik yang memenuhi persyaratan berikut:
Dukungan HTTPS, Sertifikat yang ditandatangani sendiri tidak didukung
Sertifikat SSL yang valid
Port terbuka yang menerima permintaan GET dan POST
Langkah-langkah Penyiapan
Platform Messenger Bersifat Agnostik Bahasa
Untuk mengikuti panduan ini, Anda perlu menginstal Node.js, tetapi Anda dapat menulis webhook Anda dalam bahasa server yang paling Anda sukai.
Sebelum Anda memulai, pastikan server Anda memenuhi semua persyaratan yang tercantum di atas.
1Buat proyek Node.js baru
Jalankan yang berikut ini di baris perintah untuk membuat file dan dependensi yang diperlukan:
mkdir messenger-webhook // Creates a project directory cd messenger-webhook // Navigates to the new directory touch index.js // Creates empty index.js file. npm init // Creates package.json. Accept default for all questions. npm install express body-parser --save // Installs the express.js http server framework module, // and then adds them to the dependencies section of package.json file.
Jika semua berjalan lancar, maka direktori messenger-webhook Anda akan terlihat seperti ini:
index.js node_modules package.json
2Buat server HTTP
Tambahkan kode berikut ke index.js:
'use strict';
// Imports dependencies and set up http server
const
express = require('express'),
bodyParser = require('body-parser'),
app = express().use(bodyParser.json()); // creates express http server
// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'));
Kode ini membuat server HTTP yang menampung permintaan di port default, maupun di port 1337 apabila tidak ada port default. Di panduan ini, kami menggunakan Express, sebuah a framework HTTP yang populer dan ringan, tetapi Anda dapat menggunakan framework apa pun yang Anda sukai untuk membuat webhook Anda.
3Tambahkan endpoint webhook Anda
Tambahkan kode berikut ke index.js:
// Creates the endpoint for our webhook
app.post('/webhook', (req, res) => {
let body = req.body;
// Checks this is an event from a page subscription
if (body.object === 'page') {
// Iterates over each entry - there may be multiple if batched
body.entry.forEach(function(entry) {
// Gets the message. entry.messaging is an array, but
// will only ever contain one message, so we get index 0
let webhook_event = entry.messaging[0];
console.log(webhook_event);
});
// Returns a '200 OK' response to all requests
res.status(200).send('EVENT_RECEIVED');
} else {
// Returns a '404 Not Found' if event is not from a page subscription
res.sendStatus(404);
}
});
Kode ini membuat endpoint /webhook yang menerima permintaan POST, memastikan bahwa permintaan tersebut merupakan peristiwa webhook, lalu memasukkan pesannya. Endpoint ini adalah tempat Platform Messenger mengirimkan semua peristiwa webhook.
Perhatikan bahwa endpoint ini menghasilkan tanggapan 200OK, yang memberi tahu Platform Messenger bahwa peristiwa telah diterima dan tidak perlu dikirimkan ulang. Biasanya Anda tidak akan mengirimkan tanggapan ini hingga Anda telah selesai memproses peristiwa.
4Tambahkan verifikasi webhook
Tambahkan kode berikut ke index.js:
// Adds support for GET requests to our webhook
app.get('/webhook', (req, res) => {
// Your verify token. Should be a random string.
let VERIFY_TOKEN = "<YOUR_VERIFY_TOKEN>"
// Parse the query params
let mode = req.query['hub.mode'];
let token = req.query['hub.verify_token'];
let challenge = req.query['hub.challenge'];
// Checks if a token and mode is in the query string of the request
if (mode && token) {
// Checks the mode and token sent is correct
if (mode === 'subscribe' && token === VERIFY_TOKEN) {
// Responds with the challenge token from the request
console.log('WEBHOOK_VERIFIED');
res.status(200).send(challenge);
} else {
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);
}
}
});