This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHina.py
63 lines (55 loc) · 1.97 KB
/
Hina.py
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
import logging
from os import getenv
from typing import Callable, List
from telegram.bot import Bot
from telegram.ext.callbackcontext import CallbackContext
from telegram.ext.commandhandler import CommandHandler
from telegram.ext.updater import Updater
from telegram.update import Update
from dataclasses import dataclass
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s,"
)
logger = logging.getLogger()
@dataclass
class Command:
"""Class for keeping track of a command"""
name: str
description: str
callback: Callable[[Update, CallbackContext], None]
class Hina:
def __init__(self, token: str, mode: str = "dev"):
self.bot = Bot(token=token)
self.mode = mode
self.updater = Updater(self.bot.token, use_context=True)
self.help = "Senpai, estos son los comandos u//u:"
def run(self):
self.add_command(
Command(
"taskete",
"Muestra los comandos",
lambda update, _: update.message.reply_text(self.help)
)
)
if self.mode == "dev":
self.updater.start_polling()
self.updater.idle()
elif self.mode == "prod":
PORT = int(getenv("PORT", "8443"))
HEROKU_APP_NAME = getenv("HEROKU_APP_NAME")
TOKEN = self.bot.token
self.updater.start_webhook(
listen="0.0.0.0", port=PORT, url_path=TOKEN)
self.updater.bot.set_webhook(
f"https://{HEROKU_APP_NAME}.herokuapp.com/{TOKEN}")
else:
logger.info('Mode not specified')
def add_command(self, command: Command):
self.help += f"\n /{command.name} - {command.description}"
self.updater.dispatcher.add_handler(
CommandHandler(command.name, command.callback)
)
def add_commands(self, commands: List[Command]):
for command in commands:
self.add_command(command)