forked from elliotBraem/efizzybot
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from PotLock/feat/config
Upgrade to use curate.config.json
- Loading branch information
Showing
24 changed files
with
1,198 additions
and
675 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { TransformerPlugin } from '../types/plugin'; | ||
|
||
interface Message { | ||
role: 'system' | 'user' | 'assistant'; | ||
content: string; | ||
} | ||
|
||
interface OpenRouterResponse { | ||
choices: { | ||
message: { | ||
content: string; | ||
}; | ||
}[]; | ||
} | ||
|
||
export default class GPTTransformer implements TransformerPlugin { | ||
name = 'gpt-transform'; | ||
private prompt: string = ''; | ||
private apiKey: string = ''; | ||
|
||
async initialize(config: Record<string, string>): Promise<void> { | ||
if (!config.prompt) { | ||
throw new Error('GPT transformer requires a prompt configuration'); | ||
} | ||
if (!config.apiKey) { | ||
throw new Error('GPT transformer requires an OpenRouter API key'); | ||
} | ||
this.prompt = config.prompt; | ||
this.apiKey = config.apiKey; | ||
} | ||
|
||
async transform(content: string): Promise<string> { | ||
try { | ||
const messages: Message[] = [ | ||
{ role: 'system', content: this.prompt }, | ||
{ role: 'user', content } | ||
]; | ||
|
||
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${this.apiKey}`, | ||
'HTTP-Referer': 'https://curate.fun', | ||
'X-Title': 'CurateDotFun' | ||
}, | ||
body: JSON.stringify({ | ||
model: 'openai/gpt-3.5-turbo', // Default to GPT-3.5-turbo for cost efficiency | ||
messages, | ||
temperature: 0.7, | ||
max_tokens: 1000 | ||
}) | ||
}); | ||
|
||
if (!response.ok) { | ||
const error = await response.text(); | ||
throw new Error(`OpenRouter API error: ${error}`); | ||
} | ||
|
||
const result = await response.json() as OpenRouterResponse; | ||
|
||
if (!result.choices?.[0]?.message?.content) { | ||
throw new Error('Invalid response from OpenRouter API'); | ||
} | ||
|
||
return result.choices[0].message.content; | ||
} catch (error: unknown) { | ||
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; | ||
throw new Error(`GPT transformation failed: ${errorMessage}`); | ||
} | ||
} | ||
|
||
async shutdown(): Promise<void> { | ||
// Cleanup any resources if needed | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { DistributorPlugin } from "../types/plugin"; | ||
|
||
export class TelegramPlugin implements DistributorPlugin { | ||
name = "telegram"; | ||
private botToken: string | null = null; | ||
private channelId: string | null = null; | ||
|
||
async initialize(config: Record<string, string>): Promise<void> { | ||
// Validate required config | ||
if (!config.botToken || !config.channelId) { | ||
throw new Error("Telegram plugin requires botToken and channelId"); | ||
} | ||
|
||
this.botToken = config.botToken; | ||
this.channelId = config.channelId; | ||
|
||
try { | ||
// Validate credentials | ||
const response = await fetch( | ||
`https://api.telegram.org/bot${this.botToken}/getChat?chat_id=${this.channelId}`, | ||
); | ||
if (!response.ok) { | ||
throw new Error("Failed to validate Telegram credentials"); | ||
} | ||
console.info("Telegram plugin initialized"); | ||
} catch (error) { | ||
console.error("Failed to initialize Telegram plugin:", error); | ||
throw error; | ||
} | ||
} | ||
|
||
async distribute(content: string): Promise<void> { | ||
if (!this.botToken || !this.channelId) { | ||
throw new Error("Telegram plugin not initialized"); | ||
} | ||
|
||
const message = this.formatMessage(content); | ||
await this.sendMessage(message); | ||
} | ||
|
||
private formatMessage(content: string): string { | ||
// TODO | ||
return content; | ||
} | ||
|
||
private async sendMessage(text: string): Promise<void> { | ||
const response = await fetch( | ||
`https://api.telegram.org/bot${this.botToken}/sendMessage`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
chat_id: this.channelId, | ||
text, | ||
parse_mode: "HTML", | ||
}), | ||
}, | ||
); | ||
|
||
if (!response.ok) { | ||
const error = await response.json(); | ||
throw new Error(`Telegram API error: ${JSON.stringify(error)}`); | ||
} | ||
} | ||
} |
Oops, something went wrong.