-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathplugin.py
More file actions
330 lines (272 loc) · 11.1 KB
/
plugin.py
File metadata and controls
330 lines (272 loc) · 11.1 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""
IFTTT Plugin for G-Assist - V2 SDK Version
Triggers IFTTT webhooks with gaming news integration.
"""
import os
import sys
# ============================================================================
# PATH SETUP - Must be FIRST before any third-party imports!
# ============================================================================
_plugin_dir = os.path.dirname(os.path.abspath(__file__))
_libs_path = os.path.join(_plugin_dir, "libs")
if os.path.exists(_libs_path) and _libs_path not in sys.path:
sys.path.insert(0, _libs_path)
# Now we can import third-party libraries
import json
import logging
import webbrowser
from typing import Any, Callable, Dict, List, Optional
import feedparser
import requests
try:
from gassist_sdk import Plugin
except ImportError as e:
sys.stderr.write(f"FATAL: Cannot import gassist_sdk: {e}\n")
sys.exit(1)
# ============================================================================
# CONFIGURATION
# ============================================================================
PLUGIN_NAME = "ifttt"
PLUGIN_DIR = os.path.join(
os.environ.get("PROGRAMDATA", "."),
"NVIDIA Corporation", "nvtopps", "rise", "plugins", PLUGIN_NAME
)
CONFIG_FILE = os.path.join(PLUGIN_DIR, "config.json")
LOG_FILE = os.path.join(PLUGIN_DIR, f"{PLUGIN_NAME}-plugin.log")
os.makedirs(PLUGIN_DIR, exist_ok=True)
logging.basicConfig(
filename=LOG_FILE,
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
# ============================================================================
# GLOBAL STATE
# ============================================================================
IFTTT_WEBHOOK_KEY: Optional[str] = None
EVENT_NAME: Optional[str] = None
MAIN_RSS_URL = "https://feeds.feedburner.com/ign/pc-articles"
ALTERNATE_RSS_URL = "https://feeds.feedburner.com/ign/all"
SETUP_COMPLETE = False
PENDING_CALL: Optional[Dict[str, Any]] = None # {"func": callable, "args": {...}}
def load_config():
"""Load webhook configuration."""
global IFTTT_WEBHOOK_KEY, EVENT_NAME, MAIN_RSS_URL, ALTERNATE_RSS_URL, SETUP_COMPLETE
try:
with open(CONFIG_FILE, "r") as f:
config = json.load(f)
IFTTT_WEBHOOK_KEY = config.get("webhook_key", "")
EVENT_NAME = config.get("event_name", "")
MAIN_RSS_URL = config.get("main_rss_url", MAIN_RSS_URL)
ALTERNATE_RSS_URL = config.get("alternate_rss_url", ALTERNATE_RSS_URL)
if IFTTT_WEBHOOK_KEY and len(IFTTT_WEBHOOK_KEY) > 10 and EVENT_NAME:
SETUP_COMPLETE = True
logger.info(f"Successfully loaded config from {CONFIG_FILE}")
else:
logger.warning("Webhook key or event name is empty/invalid")
IFTTT_WEBHOOK_KEY = None
EVENT_NAME = None
except FileNotFoundError:
logger.error(f"Config file not found at {CONFIG_FILE}")
except Exception as e:
logger.error(f"Error loading config: {e}")
def store_pending_call(func: Callable, **kwargs):
"""Store a function call to execute after setup completes."""
global PENDING_CALL
PENDING_CALL = {"func": func, "args": kwargs}
logger.info(f"[SETUP] Stored pending call: {func.__name__}({kwargs})")
def execute_pending_call() -> Optional[str]:
"""Execute the stored pending call if one exists. Returns result or None."""
global PENDING_CALL
if not PENDING_CALL:
return None
func = PENDING_CALL["func"]
args = PENDING_CALL["args"]
PENDING_CALL = None # Clear before executing
logger.info(f"[SETUP] Executing pending call: {func.__name__}({args})")
return func(_from_pending=True, **args)
def get_setup_instructions() -> str:
"""Return setup wizard instructions."""
return f"""_
**IFTTT Plugin - First Time Setup**
Welcome! Let's set up your IFTTT webhook. This takes about **5 minutes**.
---
**Step 1: Create IFTTT Account**
I'm opening IFTTT for you now: `https://ifttt.com/join`
1. Sign up for a free account (or log in if you already have one)
---
**Step 2: Get Webhook Key**
1. Visit `https://ifttt.com/maker_webhooks/settings`
2. Your webhook key is shown on the page
3. Copy the key (it's after "/use/")
---
**Step 3: Create an Applet**
1. Visit `https://ifttt.com/create`
2. Click **+ If This** → search for **Webhooks** → select it
3. Choose **Receive a web request**
4. Enter an event name (e.g., "gaming\\_setup")
5. Click **+ Then That** → choose your action
6. Complete the applet setup
---
**Step 4: Configure the Plugin**
I'm opening the config file for you:
```
{CONFIG_FILE}
```
Paste your values:
```
{{
"webhook_key": "your_webhook_key_here",
"event_name": "your_event_name_here",
"main_rss_url": "https://feeds.feedburner.com/ign/pc-articles",
"alternate_rss_url": "https://feeds.feedburner.com/ign/all"
}}
```
_(The plugin includes IGN gaming news headlines when triggering your applet!)_
Save the file and say **"next"** or **"continue"** when done, and I'll complete your original request.\r"""
def fetch_ign_gaming_news() -> List[str]:
"""Fetch latest gaming news from IGN RSS feed."""
try:
logger.info("Fetching IGN gaming news")
feed = feedparser.parse(MAIN_RSS_URL)
if not feed.entries:
logger.warning("No entries in main RSS feed, trying alternate")
feed = feedparser.parse(ALTERNATE_RSS_URL)
if feed.entries:
headlines = [entry.title for entry in feed.entries[:3]]
logger.info(f"Fetched {len(headlines)} headlines from IGN")
return headlines
else:
logger.error("No entries found in either RSS feed")
return []
except Exception as e:
logger.error(f"Error fetching IGN gaming news: {str(e)}")
return []
# ============================================================================
# PLUGIN SETUP
# ============================================================================
plugin = Plugin(
name=PLUGIN_NAME,
version="2.0.0",
description="IFTTT webhook trigger with gaming news"
)
# ============================================================================
# COMMANDS
# ============================================================================
@plugin.command("trigger_gaming_setup")
def trigger_gaming_setup(_from_pending: bool = False):
"""
Trigger IFTTT gaming setup applet with latest gaming news.
Args:
_from_pending: Internal flag, True when called from execute_pending_call
"""
global IFTTT_WEBHOOK_KEY, EVENT_NAME, SETUP_COMPLETE
load_config()
if not SETUP_COMPLETE or not IFTTT_WEBHOOK_KEY or not EVENT_NAME:
store_pending_call(trigger_gaming_setup)
logger.info("[COMMAND] Webhook not configured - showing setup wizard")
plugin.set_keep_session(True)
# Open IFTTT join/login page and config file for user
try:
webbrowser.open("https://ifttt.com/join")
except Exception:
pass
try:
if not os.path.exists(CONFIG_FILE):
os.makedirs(os.path.dirname(CONFIG_FILE), exist_ok=True)
with open(CONFIG_FILE, "w") as f:
json.dump({
"webhook_key": "",
"event_name": "",
"main_rss_url": "https://feeds.feedburner.com/ign/pc-articles",
"alternate_rss_url": "https://feeds.feedburner.com/ign/all"
}, f, indent=2)
os.startfile(CONFIG_FILE)
except Exception:
pass
return get_setup_instructions()
if not _from_pending:
plugin.stream("_ ") # Close engine's italic
plugin.stream("_Triggering IFTTT applet..._\n\n")
webhook_url = f"https://maker.ifttt.com/trigger/{EVENT_NAME}/with/key/{IFTTT_WEBHOOK_KEY}"
webhook_data = {}
# Fetch and include IGN news
headlines = fetch_ign_gaming_news()
if headlines:
for i, headline in enumerate(headlines[:3], 1):
webhook_data[f"value{i}"] = headline
logger.info(f"Including {len(headlines)} news headlines in webhook")
try:
if webhook_data:
response = requests.post(webhook_url, json=webhook_data, timeout=10)
else:
response = requests.post(webhook_url, timeout=10)
if 200 <= response.status_code < 300:
# Escape underscores in event name to prevent markdown formatting
safe_event_name = EVENT_NAME.replace("_", "\\_")
news_info = ""
if headlines:
news_info = f"\n\n**Headlines sent:**\n"
for headline in headlines[:3]:
# Escape underscores in headlines too
safe_headline = headline.replace("_", "\\_")
news_info += f"- {safe_headline}\n"
return f"**{safe_event_name}** triggered!{news_info}"
elif response.status_code == 401:
logger.error(f"IFTTT webhook {EVENT_NAME} failed (401): {response.text}")
return (
"**Authentication failed.**\n\n"
"Your **Webhook Key** appears to be invalid.\n\n"
f"_Config:_ `{CONFIG_FILE}`"
)
else:
logger.error(f"IFTTT webhook {EVENT_NAME} failed: {response.text}")
return (
"**Failed to trigger applet.**\n\n"
f"IFTTT returned an error _(status {response.status_code})_.\n\n"
"Please try again later."
)
except Exception as e:
logger.error(f"Error triggering IFTTT webhook {EVENT_NAME}: {str(e)}")
return (
"**Connection error.**\n\n"
"Unable to reach IFTTT. Please check your internet connection and try again."
)
@plugin.command("on_input")
def on_input(content: str = ""):
"""Handle user input during setup wizard."""
global SETUP_COMPLETE
load_config()
if SETUP_COMPLETE:
plugin.stream("_ ") # Close engine's italic
result = execute_pending_call()
if result is not None:
plugin.set_keep_session(False)
return result
else:
plugin.set_keep_session(False)
return (
"**IFTTT configured!**\n\n"
"You're all set. You can now:\n\n"
"- Trigger your **IFTTT applets** with gaming news\n"
"- Automate your gaming setup workflow"
)
else:
plugin.set_keep_session(True)
return (
"**Credentials not found.**\n\n"
"The config file is still empty or invalid.\n\n"
"---\n\n"
"Please make sure you:\n"
"1. Pasted your **Webhook Key** and **Event Name**\n"
"2. **Saved** the file\n\n"
f"_Config:_ `{CONFIG_FILE}`\n\n"
"Say **\"next\"** or **\"continue\"** when ready."
)
# ============================================================================
# MAIN
# ============================================================================
if __name__ == "__main__":
logger.info("Starting IFTTT plugin (SDK version)...")
load_config()
plugin.run()