Implement phase 3 and 4: Telegram and Twitter integration

This commit is contained in:
2026-01-18 22:42:48 +03:00
parent 836e8df344
commit f4725a5506
11 changed files with 726 additions and 7 deletions
@@ -11,6 +11,9 @@
let status = 'draft';
let loading = false;
let error = null;
let telegramPublishLoading = false;
let telegramChatId = '';
let twitterPublishLoading = false;
// Load post data if editing
onMount(async () => {
@@ -131,6 +134,97 @@
e.preventDefault();
await savePost();
}
async function publishToTelegram() {
if (!postId) {
error = 'Post must be saved before publishing to Telegram';
return;
}
if (!telegramChatId.trim()) {
error = 'Please enter a Telegram Chat ID';
return;
}
telegramPublishLoading = true;
error = null;
try {
const token = localStorage.getItem('token');
if (!token) {
throw new Error('No authentication token found');
}
const response = await fetch(`${import.meta.env.VITE_API_URL || '/api/v1'}/posts/${postId}/publish_telegram?chat_id=${encodeURIComponent(telegramChatId)}`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
if (!response.ok) {
if (response.status === 401) {
localStorage.removeItem('token');
goto('/login');
return;
}
const errorData = await response.json();
throw new Error(errorData.detail || 'Failed to publish post to Telegram');
}
const result = await response.json();
alert('Post published to Telegram successfully!');
} catch (err) {
console.error('Error publishing to Telegram:', err);
error = err.message;
} finally {
telegramPublishLoading = false;
}
}
async function publishToTwitter() {
if (!postId) {
error = 'Post must be saved before publishing to Twitter';
return;
}
twitterPublishLoading = true;
error = null;
try {
const token = localStorage.getItem('token');
if (!token) {
throw new Error('No authentication token found');
}
const response = await fetch(`${import.meta.env.VITE_API_URL || '/api/v1'}/posts/${postId}/publish_twitter`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
if (!response.ok) {
if (response.status === 401) {
localStorage.removeItem('token');
goto('/login');
return;
}
const errorData = await response.json();
throw new Error(errorData.detail || 'Failed to publish post to Twitter');
}
const result = await response.json();
alert('Post published to Twitter successfully!');
} catch (err) {
console.error('Error publishing to Twitter:', err);
error = err.message;
} finally {
twitterPublishLoading = false;
}
}
</script>
<div class="post-editor">
@@ -193,6 +287,32 @@
Cancel
</button>
</div>
{#if postId}
<div class="telegram-publish-section">
<h3>Publish to Telegram</h3>
<div class="form-group">
<label for="telegram-chat-id">Telegram Chat ID:</label>
<input
id="telegram-chat-id"
type="text"
bind:value={telegramChatId}
placeholder="Enter Telegram Chat ID or @channel_name"
disabled={telegramPublishLoading}
/>
</div>
<button type="button" on:click={publishToTelegram} disabled={telegramPublishLoading || loading}>
{telegramPublishLoading ? 'Publishing...' : 'Publish to Telegram'}
</button>
</div>
<div class="twitter-publish-section">
<h3>Publish to Twitter</h3>
<button type="button" on:click={publishToTwitter} disabled={twitterPublishLoading || loading}>
{twitterPublishLoading ? 'Publishing...' : 'Publish to Twitter'}
</button>
</div>
{/if}
</form>
</div>
@@ -269,4 +389,26 @@
border-radius: 4px;
margin-bottom: 1rem;
}
.telegram-publish-section {
margin-top: 2rem;
padding-top: 1.5rem;
border-top: 1px solid #eee;
}
.telegram-publish-section h3 {
margin-bottom: 1rem;
color: #333;
}
.twitter-publish-section {
margin-top: 2rem;
padding-top: 1.5rem;
border-top: 1px solid #eee;
}
.twitter-publish-section h3 {
margin-bottom: 1rem;
color: #333;
}
</style>