1- import { app } from '@azure/functions' ;
2-
3- import NewsFetcher from './lib/NewsFetcher' ;
4- import NewsPoster from './lib/NewsPoster' ;
5-
6- app . timer ( 'checkNewsAndPost' , {
7- schedule : '0 */5 * * * *' ,
8- handler : async ( myTimer , context ) => {
9- // お知らせを取得
10- const newsFetcher = new NewsFetcher ( ) ;
11- const newsItems = await newsFetcher . fetch ( ) ;
12-
13- // 本日のお知らせを絞り込む
14- const today = new Date ( new Date ( ) . setHours ( 0 , 0 , 0 , 0 ) ) ;
15- const latestItems = newsItems . filter ( ( item ) => item . isNewerThan ( today ) ) ;
16-
17- if ( latestItems . length === 0 ) return ;
18-
19- // お知らせを投稿
20- const newsPoster = new NewsPoster ( ) ;
21- for ( const newsItem of latestItems ) {
22- context . log ( `[New News]${ newsItem . toString ( ) } ` ) ;
23- newsPoster . post ( newsItem ) ;
24- }
25- } ,
26- } ) ;
1+ import { app , input , output , InvocationContext } from "@azure/functions" ;
2+
3+ import NewsFetcher from "./lib/NewsFetcher" ;
4+ import NewsPoster from "./lib/NewsPoster" ;
5+ import { detectNewNewsItem } from "./lib/utils" ;
6+
7+ const blobInput = input . storageBlob ( {
8+ path : "samples-workitems/newsitems.json" ,
9+ connection : "MyStorageConnectionAppSetting" ,
10+ } ) ;
11+
12+ const blobOutput = output . storageBlob ( {
13+ // 任意のパスやファイル名を設定してください
14+ path : "samples-workitems/newsitems.json" ,
15+ connection : "MyStorageConnectionAppSetting" ,
16+ } ) ;
17+
18+ app . timer ( "checkNewsAndPost" , {
19+ schedule : "0 */5 * * * *" ,
20+ handler : async ( myTimer , context : InvocationContext ) => {
21+ // 最新のお知らせを取得
22+ const newsFetcher = new NewsFetcher ( ) ;
23+ const newsItems = await newsFetcher . fetch ( ) ;
24+
25+ // 本日のお知らせを絞り込む
26+ const today = new Date ( new Date ( ) . setHours ( 0 , 0 , 0 , 0 ) ) ;
27+ const latestItems = newsItems . filter ( ( item ) => item . isNewerThan ( today ) ) ;
28+
29+ if ( latestItems . length === 0 ) return ;
30+
31+ // ストレージから本日配信したお知らせを取得
32+ const blobContent = await context . extraInputs . get ( blobInput ) ;
33+ const previousItems = blobContent ? JSON . parse ( blobContent as string ) : [ ] ;
34+
35+ // 新しいお知らせを検出
36+ const newItems = detectNewNewsItem ( previousItems , latestItems ) ;
37+
38+ // 本日のお知らせをストレージに保存
39+ const updatedItems = previousItems . concat ( newItems ) ;
40+ context . extraOutputs . set ( blobOutput , JSON . stringify ( updatedItems ) ) ;
41+
42+ // お知らせを投稿
43+ const newsPoster = new NewsPoster ( ) ;
44+ for ( const item of newItems ) {
45+ context . log ( `[New News]${ item . toString ( ) } ` ) ;
46+ newsPoster . post ( item ) ;
47+ }
48+ } ,
49+ } ) ;
0 commit comments