@@ -6,6 +6,8 @@ const util = require('hexo-util');
66const fs = require ( 'hexo-fs' ) ;
77const fetch = require ( "node-fetch" ) ;
88const url = require ( "url" )
9+ var request = require ( 'request' ) ;
10+ var moment = require ( 'moment' ) ;
911
1012// triggered after hexo generate.
1113// this output the newPost.json into public/.
@@ -27,7 +29,8 @@ hexo.on('generateAfter', async function (post) {
2729
2830//triggered before hexo deploy.
2931//it compare the newPost.json from your site and local to decide whether push the notification.
30- hexo . on ( "deployBefore" , async function ( post ) {
32+ // DeployAfter event has encode issue
33+ hexo . on ( "deployAfter" , async function ( post ) {
3134 // Get newPost.json from your site.
3235 var newPostOnlineSite = await fetch ( url . resolve ( hexo . config . url , "newPost.json" ) ) ;
3336 var newPostOnlineSite = await newPostOnlineSite . json ( ) ;
@@ -46,28 +49,56 @@ hexo.on("deployBefore", async function (post) {
4649 var payload = {
4750 title : newPostLocal . title ,
4851 message : newPostLocal . summary ,
49- target_url : new URL ( newPostLocal . url ) . pathname
52+ target_url : new URL ( newPostLocal . url ) . pathname ,
53+ // segment: [4205],
54+ send_at : moment ( ) . add ( 10 , 'minutes' ) . format ( )
55+ // sid: '4557611'
5056 } ;
51- console . log ( payload )
52- const response = await fetch (
53- "https://app.webpushr.com/api/v1/notification/send/all" ,
54- {
55- method : "POST" ,
56- headers : {
57- webpushrKey : hexo . config . webPushNotification . webpushrKey ,
58- webpushrAuthToken : hexo . config . webPushNotification . webpushrAuthToken ,
59- "Content-Type" : "application/json"
60- } ,
61- body : JSON . stringify ( payload )
57+ var headers = {
58+ webpushrKey : hexo . config . webPushNotification . webpushrKey ,
59+ webpushrAuthToken : hexo . config . webPushNotification . webpushrAuthToken ,
60+ "Content-Type" : "application/json"
61+ } ;
62+ var options = {
63+ url : 'https://api.webpushr.com/v1/notification/send/all' ,
64+ method : 'POST' ,
65+ headers : headers ,
66+ body : JSON . stringify ( payload )
67+ } ;
68+ function callback ( error , response , body ) {
69+ if ( ! error && response . statusCode == 200 ) {
70+ hexo . log . info ( "Successfully send notifications" ) ;
71+ hexo . log . info ( body ) ;
72+
73+ }
74+ else {
75+ hexo . log . error ( "Fail to send notifications" ) ;
76+ hexo . log . error ( body ) ;
6277 }
63- ) ;
64- const data = await response . json ( ) ;
65- if ( ! response . ok ) {
66- // NOT res.status >= 200 && res.status < 300
67- hexo . log . error ( "Push Notification failed " + JSON . stringify ( data ) ) ;
68- } else {
69- hexo . log . info ( "Successfully push notification" ) ;
7078 }
79+ hexo . log . info ( JSON . stringify ( payload ) )
80+ request ( options , callback ) ;
81+
82+ // const response = await fetch(
83+ // "https://app.webpushr.com/api/v1/notification/send/segment",
84+ // {
85+ // method: "POST",
86+ // headers: {
87+ // webpushrKey: hexo.config.webPushNotification.webpushrKey,
88+ // webpushrAuthToken: hexo.config.webPushNotification.webpushrAuthToken,
89+ // "Content-Type": "application/json"
90+ // },
91+ // body: JSON.stringify(payload)
92+ // }
93+ // );
94+ // // const data = await response.json();
95+ // console.log(response.status)
96+ // if (!response.ok) {
97+ // // NOT res.status >= 200 && res.status < 300
98+ // hexo.log.error("Push Notification failed " + response.status.toString()+' '+response.statusText);
99+ // } else {
100+ // hexo.log.info("Successfully push notification");
101+ // }
71102 } else {
72103 hexo . log . info ( "No New Post detected." ) ;
73104 }
0 commit comments