@@ -25,17 +25,15 @@ async def _update_terms(self) -> Dict[str, list]:
2525 two_col_org_row : Pattern [str ] = self ._compile_regex_from_parts ()
2626
2727 content = await self ._grab_data_from_github ()
28-
2928 lines : List [str ] = content .splitlines ()
3029
31- return {x ['term' ].lower (): 'f {x["term"]} is x["definition"]}' for x in
30+ return {x ['term' ].lower (): f' { x ["term" ]} is { x ["definition" ]} ' for x in
3231 self ._filter_matches (lines , two_col_org_row )}
3332
3433 async def _grab_data_from_github (self ) -> str :
3534 async with self .app .http_session .get (self .TERM_URL ) as r :
3635 r .raise_for_status ()
37- data = await r .json ()
38- return data .decode ('utf-8' )
36+ return await r .text (encoding = 'utf-8' )
3937
4038 def _compile_regex_from_parts (self ) -> Pattern [str ]:
4139 n_spaces_pipe_n_spaces = '\\ s*\\ |\\ s*'
@@ -55,64 +53,64 @@ def _filter_matches(self, lines: List[str], two_col_org_row: Pattern[str]) -> Ge
5553class TechTerms :
5654 # shared across all instances
5755 TERMS = {}
58- ADD_GITHUB_CHANCE = .25
56+ ADD_GITHUB_CHANCE = 1
5957
60- def __init__ (self , channel : str , user : str , input_text : str , user_name : str , app ):
58+ def __init__ (self , channel : str , user : str , input_text : str , app ):
6159
6260 self .channel_id = channel
6361 self .user_id = user
64- self .input_text = input_text
65- self .user_name = user_name
62+ self .input_text = self .remove_tech (input_text )
6663 self .app = app
64+ self .response_params = None
6765
68- self .response_params = self ._parse_input ()
66+ def remove_tech (self , initial_input ):
67+ return initial_input .split ('!tech' , 1 )[1 ]
6968
70- def grab_values (self ) -> dict :
69+ async def grab_values (self ) -> dict :
7170 if not self .input_text :
72- return {'type ' : 'ephemeral' , 'message' : self ._help_text ()}
71+ return {'message ' : { 'text' : self ._help_text (), 'channel' : self . channel_id ,} }
7372
7473 else :
74+ if not self .response_params :
75+ await self ._parse_input ()
7576
76- split_items : List [str ] = self .input_text .split ()
77- if split_items [0 ] == 'loud' :
78- return {'type' : 'loud' , 'message' : self ._grab_term (split_items )}
79-
80- if split_items [0 ] == 'loud' :
81- return {'type' : 'loud' , 'message' : self ._grab_term (split_items )}
77+ if self .input_text :
78+ return {'message' : self ._grab_term (term = self .input_text )}
8279
83- return {'type' : 'ephemeral' , ' message' : self ._grab_term (), }
80+ return {'message' : self ._grab_term (), }
8481
8582 async def _parse_input (self ) -> None :
86- self .TERMS = await TechTermsGrabber (self .app ).get_terms ()
83+ grabber = TechTermsGrabber (self .app )
84+ self .TERMS = await grabber .get_terms ()
8785
8886 def _help_text (self ):
8987 return ('Use this to find descriptions of common and useful tech terms. Examples:\n ' +
90- '"/resources Javascript", for self study\n ' +
91- '"/resources loud Javascript", to announce to channel' +
88+ '"!tech Java" or "!tech prolog"' +
9289 self ._source_text ())
9390
9491 def _source_text (self ):
95- return '\n For the source data please see <github| https://github.com/togakangaroo/tech-terms>'
92+ return '\n Tech Terms source: < https://github.com/togakangaroo/tech-terms|github >'
9693
97- def _convert_key_to_dict (self , key : str ) -> dict :
98- return {'term' : key , 'definition' : self .TERMS [key ]}
94+ def _convert_key_to_dict (self , key : str , random_val : bool = False ) -> dict :
95+ return {'term' : key , 'random' : random_val , ' definition' : f' { self .TERMS [key ]} ' }
9996
10097 def _grab_term (self , term = None ):
101- if isinstance ( term , list ) and len ( term ) > 1 and self .TERMS .get (term [ 1 ] .lower ()):
102- term_key : str = self . TERMS . get ( term [ 1 ] )
98+ if term and self .TERMS .get (term .lower (). strip ()):
99+ term_key : str = term . lower (). strip ( )
103100 return self ._build_response_text (self ._convert_key_to_dict (term_key ))
104101
105102 return self ._build_response_text (self ._random_term ())
106103
107104 def _build_response_text (self , term : dict ) -> dict :
108- return {'user' : self . user_id , ' channel' : self .channel_id ,
105+ return {'channel' : self .channel_id ,
109106 'text' : self ._serialize_term (term )}
110107
111108 def _random_term (self ) -> dict :
112- item = choice (self .TERMS .keys ())
113- return self ._convert_key_to_dict (item )
109+ item = choice (list ( self .TERMS .keys () ))
110+ return self ._convert_key_to_dict (item , random_val = True )
114111
115112 def _serialize_term (self , term : Dict [str , str ]) -> str :
116- addnl = self ._source_text () if random .random () < self .ADD_GITHUB_CHANCE else ''
113+ random_text = "Selected random term:\n "
114+ addnl = self ._source_text () if random () < self .ADD_GITHUB_CHANCE else ''
117115
118- return f'{ term ["definition" ]} { addnl } '
116+ return f'{ random_text if term [ "random" ] else "" } { term ["definition" ]} { addnl } '
0 commit comments