Skip to content

Commit

Permalink
pon
Browse files Browse the repository at this point in the history
  • Loading branch information
oopshnik committed Oct 18, 2024
1 parent d9081ca commit 5305673
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 38 deletions.
139 changes: 139 additions & 0 deletions botgen/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🤖</text></svg>">

<title>Telegram Bot Generator</title>
<link rel="stylesheet" href="../styles.css">

</head>
<body>

<div class="container">
<h2>Telegram Bot Generator by @oopshnik</h2>
<h3>Repo: <a href="https://github.com/oopshnik/bot_template">github.com/oopshnik/bot_template</a><br>
Console version avaible at <a href="https://github.com/oopshnik/bot_template">Github</a><br>
Template also avaible at <a href="https://github.com/oopshnik/bot_template">Github</a>.
</h3>

<input type="text" id="apiToken" placeholder="Enter your API Token" required>
<input type="text" id="botName" placeholder="Enter Bot Name" value="MyBot">
<section class="intro">
<h1>How to Setup a bot</h1>
<h3>
<br> Go to <a href="https://t.me/BotFather">@BotFather</a>
<br> Then type /newbot command
<br> Choice Bot`s Displayname and Username
<br> Then copy API-TOKEN to API_TOKEN Variable
<br> Replace code with that you need
<br> Run your code
<br> That`s all
</h3>
</section>
<textarea id="commands" placeholder="Enter custom commands (Format: command:description)" rows="5"></textarea>

<label for="enableLogging">Enable Logging?</label>
<select id="enableLogging">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>

<label for="loggingLevel">Logging Level</label>
<select id="loggingLevel">
<option value="INFO">INFO</option>
<option value="DEBUG">DEBUG</option>
<option value="WARNING">WARNING</option>
<option value="ERROR">ERROR</option>
</select>

<button onclick="generateBot()">Generate Bot</button>
</div>

<script>
function generateBot() {
const apiToken = document.getElementById('apiToken').value;
const botName = document.getElementById('botName').value;
const commandsText = document.getElementById('commands').value;
const enableLogging = document.getElementById('enableLogging').value === 'yes';
const loggingLevel = document.getElementById('loggingLevel').value;

const customCommands = commandsText
.split('\n')
.map(line => line.split(':'))
.filter(parts => parts.length === 2)
.map(parts => [parts[0].trim(), parts[1].trim()]);

const code = generateBotCode(apiToken, botName, customCommands, enableLogging, loggingLevel);

const blob = new Blob([code], { type: 'text/x-python' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = `${botName.toLowerCase().replace(/ /g, '_')}.py`;
link.click();
}

function generateBotCode(apiToken, botName, customCommands, enableLogging, loggingLevel) {
const loggingSetup = enableLogging ? `
# Logging configuration
logging.basicConfig(
level=logging.${loggingLevel.toUpperCase()},
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
filename='${botName.toLowerCase()}_bot.log',
filemode='a'
)
logger = logging.getLogger(__name__)
` : "logger = None";

const commandsCode = customCommands.map(([command, desc]) => `
@bot.message_handler(commands=['${command}'])
def ${command}(message):
${enableLogging ? `logger.info(f"User {message.from_user.id} requested ${command}")` : ''}
bot.reply_to(message, "${desc}")
`).join('\n');

return `
import os
import telebot
import logging
${loggingSetup}
API_TOKEN = '${apiToken}'
bot = telebot.TeleBot(API_TOKEN)
@bot.message_handler(commands=['start'])
def welcome(message):
${enableLogging ? `logger.info(f"User {message.from_user.id} started the bot")` : ''}
bot.reply_to(message, f"Welcome {message.from_user.first_name}! Use /help to see available commands.")
@bot.message_handler(commands=['help'])
def help(message):
${enableLogging ? `logger.info(f"User {message.from_user.id} requested help")` : ''}
help_message = "Available commands:\\n" + "\\n".join([
"/{0} - {1}".format(cmd, desc) for cmd, desc in ${JSON.stringify(customCommands)}
])
bot.reply_to(message, help_message)
${commandsCode}
@bot.message_handler(func=lambda message: True)
def echo_all(message):
${enableLogging ? `logger.info(f"Received message: {message.text}")` : ''}
bot.reply_to(message, "I don't understand that command. Use /help.")
if __name__ == '__main__':
${enableLogging ? `logger.info("${botName} bot started")` : ''}
try:
bot.polling(none_stop=True)
except Exception as e:
${enableLogging ? `logger.error(f"Error: {e}")` : ''}
finally:
${enableLogging ? `logger.info("${botName} bot stopped")` : ''}
`;
}
</script>

</body>
</html>
1 change: 0 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ <h2>Ку </h2>
<li>🌱 Frontend enjoyer</li>
<li>🐧 Linux User</li>

<li><a href="./posts.html">🤓 Блог и т.д на страничке</a></li>


</ul>
Expand Down
34 changes: 0 additions & 34 deletions posts.html

This file was deleted.

83 changes: 80 additions & 3 deletions styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Original Styles (Kept as is) */
body {
margin: 0;
padding: 0;
Expand Down Expand Up @@ -31,12 +32,13 @@ header h1 {
margin-top: 2rem;
font-size: 1.5rem;
}

.links ul {
display: flex;
gap: 20px; /* Расстояние между ссылками */
flex-wrap: wrap; /* Чтобы ссылки не выходили за пределы экрана на мобильных устройствах */
gap: 20px;
flex-wrap: wrap;
padding: 0;
list-style: none; /* Убираем маркеры списка */
list-style: none;
}

.links ul li {
Expand Down Expand Up @@ -74,3 +76,78 @@ footer p {
text-align: center;
color: #888;
}

/* New Styles for Forms, Inputs, and Buttons */

form {
margin-top: 2rem;
display: flex;
flex-direction: column;
gap: 1rem;
}

input[type="text"],
input[type="password"],
textarea {
width: 100%;
padding: 0.75rem;
border: 1px solid #87CEEB;
border-radius: 5px;
background-color: #1E1E1E;
color: #e0e0e0;
font-size: 1rem;
transition: border-color 0.3s ease-in-out;
}

input[type="text"]:focus,
input[type="password"]:focus,
textarea:focus {
border-color: #00BFFF;
outline: none;
}

button {
padding: 0.75rem 1.5rem;
font-size: 1rem;
background-color: #87CEEB;
color: #121212;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease-in-out;
}

button:hover {
background-color: #00BFFF;
}

button:disabled {
background-color: #555;
cursor: not-allowed;
}

label {
font-size: 1rem;
color: #e0e0e0;
}

textarea {
resize: none;
height: 100px;
}

.error-message {
color: #FF6B6B;
font-size: 0.9rem;
}

.success-message {
color: #32CD32;
font-size: 0.9rem;
}

@media (max-width: 600px) {
input, button, textarea {
font-size: 0.9rem;
}
}

0 comments on commit 5305673

Please sign in to comment.