genstava789 commited on
Commit
bfc4e9f
·
1 Parent(s): 218d0d7
Files changed (2) hide show
  1. bot.py +5 -3
  2. database.py +15 -6
bot.py CHANGED
@@ -56,7 +56,7 @@ cookies_file = None
56
  cwd = Path.cwd()
57
 
58
  def fetch_ngrok_data(user_id):
59
- api_key = user_api_keys.get(user_id, default_api_key)
60
  if not api_key:
61
  return None, "Ngrok API key not found. Please set your Ngrok API key."
62
  headers = {
@@ -64,13 +64,15 @@ def fetch_ngrok_data(user_id):
64
  "Ngrok-Version": "2"
65
  }
66
  try:
67
- response = requests.get("https://api.ngrok.com/tunnels", headers=headers)
 
68
  if response.status_code == 403:
69
  return None, "Ngrok API key is invalid or does not have permission to access tunnels. Please set a valid API key."
70
  response.raise_for_status()
71
  return response.json().get("tunnels", []), None
72
  except requests.RequestException as e:
73
  return None, f"Error fetching Ngrok data: {str(e)}"
 
74
 
75
  def get_ngrok_tunnels(user_id):
76
  tunnels, error = fetch_ngrok_data(user_id)
@@ -152,7 +154,7 @@ async def capture_api_key(client: Client, message: Message):
152
  if message.reply_to_message and message.reply_to_message.text == "Please send your Ngrok API key as a reply to this message.":
153
  user_id = message.from_user.id
154
  api_key = message.text.strip()
155
- user_api_keys[user_id] = api_key
156
  await message.reply("Ngrok API key set successfully!")
157
  await message.reply_to_message.delete()
158
 
 
56
  cwd = Path.cwd()
57
 
58
  def fetch_ngrok_data(user_id):
59
+ api_key = db.get_api_key(user_id)
60
  if not api_key:
61
  return None, "Ngrok API key not found. Please set your Ngrok API key."
62
  headers = {
 
64
  "Ngrok-Version": "2"
65
  }
66
  try:
67
+ response = requests.get(
68
+ "https://api.ngrok.com/tunnels", headers=headers)
69
  if response.status_code == 403:
70
  return None, "Ngrok API key is invalid or does not have permission to access tunnels. Please set a valid API key."
71
  response.raise_for_status()
72
  return response.json().get("tunnels", []), None
73
  except requests.RequestException as e:
74
  return None, f"Error fetching Ngrok data: {str(e)}"
75
+
76
 
77
  def get_ngrok_tunnels(user_id):
78
  tunnels, error = fetch_ngrok_data(user_id)
 
154
  if message.reply_to_message and message.reply_to_message.text == "Please send your Ngrok API key as a reply to this message.":
155
  user_id = message.from_user.id
156
  api_key = message.text.strip()
157
+ db.insert_api_key(user_id, api_key)
158
  await message.reply("Ngrok API key set successfully!")
159
  await message.reply_to_message.delete()
160
 
database.py CHANGED
@@ -1,7 +1,6 @@
1
  from pymongo import MongoClient
2
  from bson.objectid import ObjectId
3
 
4
-
5
  class MongoDB:
6
  def __init__(self, uri, db_name):
7
  self.client = MongoClient(uri)
@@ -16,8 +15,7 @@ class MongoDB:
16
  return video_detail
17
 
18
  def update_video_detail(self, video_id, update_data):
19
- result = self.db.videos.update_one(
20
- {"_id": ObjectId(video_id)}, {"$set": update_data})
21
  return result.modified_count
22
 
23
  def delete_video_detail(self, video_id):
@@ -28,15 +26,26 @@ class MongoDB:
28
  result = self.db.chats.insert_one(chat_detail)
29
  return str(result.inserted_id)
30
 
31
- def get_chat_detal(self, chat_id):
32
  chat_detail = self.db.chats.find_one({"_id": ObjectId(chat_id)})
33
  return chat_detail
34
 
35
  def update_chat_detail(self, chat_id, update_data):
36
- result = self.db.chats.update_one(
37
- {"_id": ObjectId(chat_id)}, {"$set": update_data})
38
  return result.modified_count
39
 
40
  def delete_chat_detail(self, chat_id):
41
  result = self.db.chats.delete_one({"_id": ObjectId(chat_id)})
42
  return result.deleted_count
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from pymongo import MongoClient
2
  from bson.objectid import ObjectId
3
 
 
4
  class MongoDB:
5
  def __init__(self, uri, db_name):
6
  self.client = MongoClient(uri)
 
15
  return video_detail
16
 
17
  def update_video_detail(self, video_id, update_data):
18
+ result = self.db.videos.update_one({"_id": ObjectId(video_id)}, {"$set": update_data})
 
19
  return result.modified_count
20
 
21
  def delete_video_detail(self, video_id):
 
26
  result = self.db.chats.insert_one(chat_detail)
27
  return str(result.inserted_id)
28
 
29
+ def get_chat_detail(self, chat_id):
30
  chat_detail = self.db.chats.find_one({"_id": ObjectId(chat_id)})
31
  return chat_detail
32
 
33
  def update_chat_detail(self, chat_id, update_data):
34
+ result = self.db.chats.update_one({"_id": ObjectId(chat_id)}, {"$set": update_data})
 
35
  return result.modified_count
36
 
37
  def delete_chat_detail(self, chat_id):
38
  result = self.db.chats.delete_one({"_id": ObjectId(chat_id)})
39
  return result.deleted_count
40
+
41
+ def insert_api_key(self, user_id, api_key):
42
+ result = self.db.api_keys.update_one(
43
+ {"user_id": user_id},
44
+ {"$set": {"api_key": api_key}},
45
+ upsert=True
46
+ )
47
+ return result.upserted_id or result.modified_count
48
+
49
+ def get_api_key(self, user_id):
50
+ api_key_doc = self.db.api_keys.find_one({"user_id": user_id})
51
+ return api_key_doc["api_key"] if api_key_doc else None