-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_render.py
More file actions
188 lines (165 loc) Β· 5.74 KB
/
Copy pathdeploy_render.py
File metadata and controls
188 lines (165 loc) Β· 5.74 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
"""
Deploy Telegram Bot to Render.com using API
"""
import requests
import json
from pathlib import Path
# Render API Configuration
API_KEY = 'rnd_NFW3mtRJty97THVedLHx3Rpr3mL6'
PROJECT_NAME = 'API_bot'
BASE_URL = 'https://api.render.com/v1'
HEADERS = {
'Authorization': f'Bearer {API_KEY}',
'Content-Type': 'application/json'
}
def check_services():
"""List all existing services"""
print('π Checking existing services...')
response = requests.get(
f'{BASE_URL}/services',
headers=HEADERS
)
if response.status_code == 200:
services = response.json()
print(f'β Found {len(services)} service(s)')
for service in services:
print(f" - {service.get('name')} ({service.get('id')})")
return services
else:
print(f'β Error: {response.status_code} - {response.text}')
return []
def create_service():
"""Create new Render service"""
print(f'\nπ Creating service: {PROJECT_NAME}...')
# Read environment variables from .env
env_vars = []
env_file = Path(__file__).parent / '.env'
if env_file.exists():
with open(env_file, 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
env_vars.append({
'key': key.strip(),
'value': value.strip()
})
service_data = {
'type': 'web_service',
'name': PROJECT_NAME,
'runtime': 'python',
'plan': 'free',
'buildCommand': 'pip install -r requirements.txt',
'startCommand': 'python bot.py',
'envVars': env_vars,
'autoDeploy': True,
'serviceDetails': {
'disk': None,
'env': 'python',
'envSpecificDetails': {
'pythonVersion': '3.11'
}
}
}
response = requests.post(
f'{BASE_URL}/services',
headers=HEADERS,
json=service_data
)
if response.status_code in [200, 201]:
service = response.json()
print(f'β Service created successfully!')
print(f" ID: {service.get('id')}")
print(f" Name: {service.get('name')}")
print(f" URL: {service.get('url', 'N/A')}")
return service
else:
print(f'β Error creating service: {response.status_code}')
print(f' Response: {response.text}')
return None
def deploy_from_github(repo_url):
"""Deploy from GitHub repository"""
print(f'\nπ¦ Deploying from GitHub: {repo_url}...')
service_data = {
'type': 'web_service',
'name': PROJECT_NAME,
'runtime': 'python',
'plan': 'free',
'repo': repo_url,
'buildCommand': 'pip install -r requirements.txt',
'startCommand': 'python bot.py',
'autoDeploy': True
}
response = requests.post(
f'{BASE_URL}/services',
headers=HEADERS,
json=service_data
)
if response.status_code in [200, 201]:
service = response.json()
print(f'β Service deployed!')
print(f" Dashboard: https://dashboard.render.com/web/{service.get('id')}")
return service
else:
print(f'β Error: {response.status_code} - {response.text}')
return None
def get_owner_id():
"""Get owner ID for account"""
print('\nπ Getting owner ID...')
response = requests.get(
f'{BASE_URL}/owners',
headers=HEADERS
)
if response.status_code == 200:
owners = response.json()
if owners:
owner_id = owners[0].get('id')
print(f'β Owner ID: {owner_id}')
return owner_id
print(f'β Error getting owner: {response.status_code}')
return None
def main():
print('π¨ Render.com Deployment Tool\n')
print(f'Project: {PROJECT_NAME}')
print(f'API Key: {API_KEY[:20]}...\n')
# Check existing services
services = check_services()
# Get owner ID
owner_id = get_owner_id()
if not owner_id:
print('\nβ οΈ Could not get owner ID. Please deploy manually:')
print(' 1. Go to https://dashboard.render.com')
print(' 2. Click "New +" β "Web Service"')
print(' 3. Connect your GitHub repository')
print(' 4. Configure:')
print(f' - Name: {PROJECT_NAME}')
print(' - Runtime: Python 3')
print(' - Build Command: pip install -r requirements.txt')
print(' - Start Command: python bot.py')
print(' - Plan: Free')
print(' 5. Add TELEGRAM_BOT_TOKEN environment variable')
return
print('\nπ Deployment Options:')
print('1. Manual deployment (recommended)')
print('2. Deploy from GitHub (requires repo URL)')
print('\nNote: Render requires GitHub/GitLab repository for deployments.')
print('API-only deployments without git are not supported.\n')
print('β
Next Steps:')
print('1. Push code to GitHub:')
print(' git init')
print(' git add .')
print(' git commit -m "Deploy bot"')
print(' git remote add origin https://github.com/YOUR_USERNAME/telegram-bot.git')
print(' git push -u origin main')
print()
print('2. Deploy on Render Dashboard:')
print(' β https://dashboard.render.com/create?type=web')
print(f' β Connect your GitHub repo')
print(f' β Name: {PROJECT_NAME}')
print(' β Runtime: Python 3')
print(' β Build: pip install -r requirements.txt')
print(' β Start: python bot.py')
print()
print('Your bot will be live in ~2 minutes! π')
if __name__ == '__main__':
main()