File size: 2,282 Bytes
24e0afd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from shiny import reactive, render, ui
import os
import tweepy
from dotenv import load_dotenv
from llm_connect import get_response  # your existing function

load_dotenv()

# Twitter API v2 credentials
BEARER_TOKEN = os.getenv("TWITTER_BEARER_TOKEN")
API_KEY = os.getenv("TWITTER_ACC_API_KEY")
API_SECRET = os.getenv("TWITTER_ACC_API_SECRET")
ACCESS_TOKEN = os.getenv("TWITTER_ACC_ACCESS_TOKEN")
ACCESS_TOKEN_SECRET = os.getenv("TWITTER_ACC_ACCESS_TOKEN_SECRET")

client = tweepy.Client(
    bearer_token=BEARER_TOKEN,
    consumer_key=API_KEY,
    consumer_secret=API_SECRET,
    access_token=ACCESS_TOKEN,
    access_token_secret=ACCESS_TOKEN_SECRET
)

generated_tweet = reactive.Value("")

def generate_tweet_from_topic(topic: str) -> str:
    prompt = (
        f"You are a social media manager for a hobby e-commerce company called 'Ultima Supply'.\n"
        f"Write a short, engaging Twitter post (max 500 characters) about: '{topic}'.\n"
        f"Include emojis or 3-5 SEO relevant hashtags. Use casual, fun language."
    )
    return get_response(
        input=prompt,
        template=lambda x: x,
        llm='gemini',
        md=False,
        temperature=0.9,
        max_tokens=500
    )

def post_tweet(text: str) -> str:
    try:
        response = client.create_tweet(text=text)
        return f"✅ Tweet posted (ID: {response.data['id']})"
    except Exception as e:
        return f"❌ Failed to post tweet: {e}"

def server(input, output, session):
    post_status = reactive.Value("")

    @output
    @render.ui
    @reactive.event(input.gen_btn_twt)
    def tweet_draft():
        topic = input.tweet_topic()
        if not topic.strip():
            return ui.HTML("<p><strong>⚠️ Enter a topic first.</strong></p>")
        tweet = generate_tweet_from_topic(topic)
        generated_tweet.set(tweet)
        return ui.HTML(f"<p><strong>Generated Tweet:</strong><br>{tweet}</p>")

    @output
    @render.text
    def tweet_post_status():
        return post_status()

    @reactive.effect
    @reactive.event(input.post_btn_twt)
    def _():
        tweet = generated_tweet()
        if not tweet:
            post_status.set("⚠️ No tweet generated yet.")
        else:
            result = post_tweet(tweet)
            post_status.set(result)