-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathdilbert.js
More file actions
100 lines (85 loc) · 2.62 KB
/
Copy pathdilbert.js
File metadata and controls
100 lines (85 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env node
/**
* Dilbert Skill for Clawdbot
* Fetches and sends random Dilbert comics
*/
const { spawn } = require('child_process');
const path = require('path');
const SKILL_NAME = 'dilbert';
const SKILL_DIR = __dirname;
/**
* Execute the skill.sh script to fetch a Dilbert comic
* @returns {Promise<string>} Path to the downloaded comic image
*/
async function fetchDilbertComic() {
return new Promise((resolve, reject) => {
const scriptPath = path.join(SKILL_DIR, 'skill.sh');
const process = spawn(scriptPath, [], {
cwd: SKILL_DIR,
env: process.env
});
let stdout = '';
let stderr = '';
process.stdout.on('data', (data) => {
stdout += data.toString();
});
process.stderr.on('data', (data) => {
stderr += data.toString();
});
process.on('close', (code) => {
if (code === 0) {
const imagePath = stdout.trim();
resolve(imagePath);
} else {
reject(new Error(`Failed to fetch Dilbert comic. Exit code: ${code}. Stderr: ${stderr}`));
}
});
process.on('error', (err) => {
reject(err);
});
});
}
/**
* Main skill handler - called by Clawdbot
* @param {Object} args - Arguments from Clawdbot (may include context, message info, etc.)
* @returns {Promise<Object>} Result object with image path and metadata
*/
async function skillHandler(args) {
try {
console.log(`[${SKILL_NAME}] Fetching random Dilbert comic...`);
const imagePath = await fetchDilbertComic();
return {
success: true,
skill: SKILL_NAME,
imagePath: imagePath,
message: 'Here\'s a random Dilbert comic for you!',
type: 'image'
};
} catch (error) {
console.error(`[${SKILL_NAME}] Error:`, error.message);
return {
success: false,
skill: SKILL_NAME,
error: error.message,
message: 'Sorry, I couldn\'t fetch a Dilbert comic at this time.'
};
}
}
// Export for Clawdbot
module.exports = {
name: SKILL_NAME,
handler: skillHandler,
fetch: fetchDilbertComic
};
// If run directly (for testing)
if (require.main === module) {
skillHandler({})
.then((result) => {
console.log('Result:', JSON.stringify(result, null, 2));
process.exit(result.success ? 0 : 1);
})
.catch((err) => {
console.error('Fatal error:', err);
process.exit(1);
});
}