-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
executable file
·103 lines (75 loc) · 3.66 KB
/
bot.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
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
import os
import logging
import hashlib
from datetime import datetime
from message_creator import MessageCreator
from configs import WEBHOOK_BASE_URL, BOT_SECRET, KEY, BOT_ADMIN_ID
from telegram import Update
from telegram.ext import Application, CommandHandler
from telegram.constants import ParseMode
logger = logging.getLogger(__name__)
message_creator = MessageCreator()
url_prefix_gitlab = WEBHOOK_BASE_URL + "/gitlab/"
url_prefix_github = WEBHOOK_BASE_URL + "/github/"
class BotContainer(object):
pass
bot_container = BotContainer()
def get_token(id):
hash_object = hashlib.md5((str(id) + KEY).encode())
hex_dig = hash_object.hexdigest()
return str(hex_dig)
async def new_gitlab(update, context, log_the_event=True):
bot = update.get_bot()
id = update.message.chat_id
if id < 0:
id = "n" + str(-id)
else:
id = "p" + str(id)
if log_the_event:
await log_text("new_gitlab " + str(update.message.chat_id) + " " + str(update.message.from_user.username) + " " + str(update.message.chat.title), bot)
await bot.sendMessage(update.message.chat_id,
text=message_creator.new_gitlab(url_prefix_gitlab + id, get_token(id)), parse_mode=ParseMode.HTML)
async def new_github(update, context, log_the_event=True):
bot = update.get_bot()
id = update.message.chat_id
if id < 0:
id = "n" + str(-id)
else:
id = "p" + str(id)
if log_the_event:
await log_text("new_github " + str(update.message.chat_id) + " " + str(update.message.from_user.username) + " " + str(update.message.chat.title), bot)
await bot.sendMessage(update.message.chat_id,
text=message_creator.new_github(url_prefix_github + id, get_token(id)), parse_mode=ParseMode.HTML)
async def start(update, context):
bot = update.get_bot()
await log_text("start " + str(update.message.chat_id) + " " + str(update.message.from_user.username) + " " + str(update.message.chat.title), bot)
await new_gitlab(update, context, log_the_event=False)
await new_github(update, context, log_the_event=False)
async def help_gitlab(update, context):
bot = update.get_bot()
await log_text("help_gitlab " + str(update.message.chat_id) + " " + str(update.message.from_user.username) + " " + str(update.message.chat.title), bot)
await bot.sendMessage(update.message.chat_id, text=message_creator.help_gitlab(), parse_mode=ParseMode.MARKDOWN)
async def help_github(update, context):
bot = update.get_bot()
await log_text("help_github " + str(update.message.chat_id) + " " + str(update.message.from_user.username) + " " + str(update.message.chat.title), bot)
await bot.sendMessage(update.message.chat_id, text=message_creator.help_github(), parse_mode=ParseMode.MARKDOWN)
async def log_text(line, bot=None):
try:
if bot:
await bot.sendMessage(BOT_ADMIN_ID, text=(str(datetime.now()) + " " + str(line)))
except:
pass
with open(os.path.join(os.path.dirname(__file__), "logs.txt"), "a") as f:
f.write(str(datetime.now()) + " " + str(line) + "\n")
async def create_bot():
bot_app = (
Application.builder().token(BOT_SECRET).updater(None).build()
)
bot_app.add_handler(CommandHandler("start", start))
bot_app.add_handler(CommandHandler("new_gitlab", new_gitlab))
bot_app.add_handler(CommandHandler("new_github", new_github))
bot_app.add_handler(CommandHandler("help_gitlab", help_gitlab))
bot_app.add_handler(CommandHandler("help_github", help_github))
await bot_app.bot.set_webhook(url=f"{WEBHOOK_BASE_URL}/telegram", allowed_updates=Update.ALL_TYPES)
bot_container.bot = bot_app.bot
return bot_app