ส่งการแจ้งเตือนผ่าน API
POST https://notification.nsru.ac.th/api/send-notify
Authorization: Bearer <ACCESS_TOKEN>
Content-Type: application/json
Accept: application/json
Request body
| ฟิลด์ | ชนิด | จำเป็น | คำอธิบาย |
|---|---|---|---|
to | string | ✅ | NSRU Account ของผู้รับ ไม่ต้องมีโดเมน เช่น napadon.k |
message | string | ✅ | ข้อความแจ้งเตือน |
notify_type | string | ชื่อประเภทแจ้งเตือนของโปรเจกต์ | |
action_url | string | ลิงก์ที่เกี่ยวข้อง ส่งต่อไปยัง Web Push และ Custom Webhook |
{
"to": "napadon.k",
"message": "มีหนังสือเวียนใหม่ เรื่อง ...",
"notify_type": "หนังสือเวียน",
"action_url": "https://edoc.nsru.ac.th/document/12345"
}
Response
สำเร็จ — ข้อความเข้าคิวรอส่งแล้ว
{
"is_success": true,
"activity_batch_uuid": "9b1d6c1e-....",
"project_name": "e-Document"
}
ไม่สำเร็จ — ส่วนใหญ่เกิดจากไม่พบผู้ใช้ตาม to (ผู้รับยังไม่เคยเข้าสู่ระบบ NSRU Notification)
{
"is_success": false,
"message": "ไม่สามารถส่ง Notify ได้",
"project_name": "e-Document"
}
| HTTP status | ความหมาย |
|---|---|
200 | ตรวจสอบ is_success ใน body |
401 | ไม่ได้แนบ Token หรือ Token ไม่ถูกต้อง/ถูกรีเซ็ตแล้ว |
note
is_success: true หมายถึงรับข้อความเข้าคิวแล้ว การส่งจริงเกิดขึ้นเบื้องหลัง ตรวจสอบผลการส่งได้ที่ติดตามสถานะการส่ง
ตัวอย่างโค้ด
cURL
curl -X POST https://notification.nsru.ac.th/api/send-notify \
-H "Authorization: Bearer $NSRU_NOTIFY_TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d '{"to":"napadon.k","message":"ทดสอบการแจ้งเตือน"}'
PHP (Laravel)
use Illuminate\Support\Facades\Http;
$response = Http::withToken(env('NSRU_NOTIFY_TOKEN'))
->acceptJson()
->post('https://notification.nsru.ac.th/api/send-notify', [
'to' => 'napadon.k',
'message' => 'ทดสอบการแจ้งเตือน',
'action_url' => 'https://example.nsru.ac.th',
]);
if (! $response->json('is_success')) {
logger()->warning('NSRU Notify failed', $response->json());
}
JavaScript (Node.js 18+)
const res = await fetch('https://notification.nsru.ac.th/api/send-notify', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.NSRU_NOTIFY_TOKEN}`,
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify({ to: 'napadon.k', message: 'ทดสอบการแจ้งเตือน' }),
});
const data = await res.json();
Python
import os, requests
r = requests.post(
"https://notification.nsru.ac.th/api/send-notify",
headers={"Authorization": f"Bearer {os.environ['NSRU_NOTIFY_TOKEN']}", "Accept": "application/json"},
json={"to": "napadon.k", "message": "ทดสอบการแจ้งเตือน"},
timeout=10,
)
print(r.json())
caution
เรียก API จากฝั่งเซิร์ฟเวอร์เท่านั้น ห้ามเรียกจาก JavaScript บนเบราว์เซอร์ เพราะจะทำให้ Access Token รั่วไหล