1
+
2
+ import moment from "moment" ;
3
+
4
+ import { HostServices , Logger , Loop , serveLoop } from '../../../dist' ;
5
+ import {
6
+ WhisperDisambiguationElements ,
7
+ // WhisperDisambiguationOption,
8
+ } from '../../../dist/hostClients/whisperService' ;
9
+
10
+ import { RecallJSON } from './transform'
11
+
12
+ const logger = new Logger ( 'olive-helps-node-example-network' ) ;
13
+
14
+ export interface Element {
15
+ [ key : string ] : WhisperDisambiguationElements
16
+ }
17
+
18
+ class ExampleLoop implements Loop {
19
+ private _host : HostServices | undefined ;
20
+
21
+ start ( host : HostServices ) : void {
22
+ this . _host = host ;
23
+ const now = moment ( ) ;
24
+ const url = `https://api.fda.gov/food/enforcement.json?search=report_date:[${ now . subtract ( 3 , 'months' ) . startOf ( 'month' ) . format ( 'YYYYMMDD' ) } +TO+${ now . endOf ( "month" ) . format ( 'YYYYMMDD' ) } ]&limit=10` ;
25
+
26
+ logger . info ( 'Emitting list whisper' , url ) ;
27
+
28
+ try {
29
+ this . host . network . httpRequest ( {
30
+ url,
31
+ method : "GET" ,
32
+ body : "" ,
33
+ } ) . then ( ( response ) => {
34
+ const { results } = JSON . parse ( Buffer . from ( response . data ) . toString ( 'utf8' ) ) ;
35
+ const itemList = { } as Element ;
36
+
37
+ results . forEach ( ( resultItem : RecallJSON , index : number ) => {
38
+ itemList [ resultItem . recall_number ] = {
39
+ label : `😝${ resultItem . recalling_firm } (${ resultItem . recall_initiation_date } ) jb` ,
40
+ order : index + 1 ,
41
+ type : 'option' ,
42
+ }
43
+ } )
44
+
45
+ this . host . whisper . disambiguationWhisper ( {
46
+ label : 'Latest FDA Food Recall' ,
47
+ markdown : '' ,
48
+ elements : itemList ,
49
+ } ,
50
+ // I don't know what goes here (An argument for 'listener' was not provided.)
51
+ ) ;
52
+ } )
53
+ } catch ( e ) {
54
+ logger . error ( 'Error using network service' , 'error' , e . toString ( ) ) ;
55
+ }
56
+ }
57
+
58
+ stop ( ) : void {
59
+ logger . info ( 'Stopping' ) ;
60
+ this . _host = undefined ;
61
+ process . exit ( 0 ) ;
62
+ }
63
+
64
+ private get host ( ) : HostServices {
65
+ if ( this . _host == null ) {
66
+ throw new Error ( 'Cannot Retrieve Host Before Set' ) ;
67
+ }
68
+ return this . _host ;
69
+ }
70
+ }
71
+
72
+ const loop = new ExampleLoop ( ) ;
73
+ serveLoop ( loop ) ;
0 commit comments