add configurable ad-hoc post interval
This commit is contained in:
		
							
								
								
									
										68
									
								
								src/main.ts
									
									
									
									
									
								
							
							
						
						
									
										68
									
								
								src/main.ts
									
									
									
									
									
								
							| @ -39,6 +39,9 @@ export const envConfig = { | ||||
|     ? parseInt(process.env.FETCH_INTERVAL) | ||||
|     : 15000, | ||||
|   bearerToken: process.env.INSTANCE_BEARER_TOKEN || "", | ||||
|   adHocPostInterval: process.env.RANDOM_POST_INTERVAL | ||||
|     ? parseInt(process.env.RANDOM_POST_INTERVAL) | ||||
|     : 3600000, | ||||
| }; | ||||
|  | ||||
| const ollamaConfig: OllamaConfigOptions = { | ||||
| @ -138,6 +141,57 @@ const postReplyToStatus = async ( | ||||
|   } | ||||
| }; | ||||
|  | ||||
| const createTimelinePost = async () => { | ||||
|   const { | ||||
|     bearerToken, | ||||
|     ollamaModel, | ||||
|     ollamaSystemPrompt, | ||||
|     ollamaUrl, | ||||
|     pleromaInstanceUrl, | ||||
|   } = envConfig; | ||||
|   const ollamaRequestBody: OllamaRequest = { | ||||
|     model: ollamaModel, | ||||
|     prompt: "Make a random post about a random topic.", | ||||
|     system: ollamaSystemPrompt, | ||||
|     stream: false, | ||||
|     // options: ollamaConfig, | ||||
|   }; | ||||
|   try { | ||||
|     const response = await fetch(`${ollamaUrl}/api/generate`, { | ||||
|       method: "POST", | ||||
|       body: JSON.stringify(ollamaRequestBody), | ||||
|     }); | ||||
|     if (!response.ok) | ||||
|       throw new Error("Error generating ad-hoc Ollama response"); | ||||
|  | ||||
|     const ollamaResponse: OllamaResponse = await response.json(); | ||||
|  | ||||
|     const newStatusBody: NewStatusBody = { | ||||
|       content_type: "text/markdown", | ||||
|       status: ollamaResponse.response, | ||||
|     }; | ||||
|  | ||||
|     const pleromaResponse = await fetch( | ||||
|       `${pleromaInstanceUrl}/api/v1/statuses`, | ||||
|       { | ||||
|         method: "POST", | ||||
|         headers: { | ||||
|           "Content-Type": "application/json", | ||||
|           Authorization: `Bearer ${bearerToken}`, | ||||
|         }, | ||||
|         body: JSON.stringify(newStatusBody), | ||||
|       } | ||||
|     ); | ||||
|  | ||||
|     if (!pleromaResponse.ok) | ||||
|       throw new Error("Error posting ad-hoc Ollama response to Pleroma"); | ||||
|   } catch (error: unknown) { | ||||
|     if (error instanceof Error) { | ||||
|       throw new Error(error.message); | ||||
|     } | ||||
|   } | ||||
| }; | ||||
|  | ||||
| let notifications = []; | ||||
| const beginFetchCycle = async () => { | ||||
|   setInterval(async () => { | ||||
| @ -159,6 +213,18 @@ const beginFetchCycle = async () => { | ||||
|   }, envConfig.fetchInterval); // lower intervals may cause the bot to respond multiple times to the same message, but we try to mitigate this with the deleteNotification function | ||||
| }; | ||||
|  | ||||
| const beginStatusPostInterval = async () => { | ||||
|   setInterval(async () => { | ||||
|     try { | ||||
|       createTimelinePost(); | ||||
|     } catch (error: unknown) { | ||||
|       if (error instanceof Error) { | ||||
|         throw new Error(error.message); | ||||
|       } | ||||
|     } | ||||
|   }, envConfig.adHocPostInterval); | ||||
| }; | ||||
|  | ||||
| console.log( | ||||
|   `Fetching notifications from ${envConfig.pleromaInstanceDomain}, every ${ | ||||
|     envConfig.fetchInterval / 1000 | ||||
| @ -173,4 +239,6 @@ console.log( | ||||
|   )}` | ||||
| ); | ||||
| console.log(`System prompt: ${envConfig.ollamaSystemPrompt}`); | ||||
|  | ||||
| await beginFetchCycle(); | ||||
| await beginStatusPostInterval(); | ||||
|  | ||||
		Reference in New Issue
	
	Block a user