Last active
December 16, 2021 20:32
-
-
Save stypr/27be3a23a2091dd6245f5996e7569065 to your computer and use it in GitHub Desktop.
Bugcamp.io Telegram Bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from telegram.ext import Updater | |
from telegram.ext import CommandHandler | |
import telegram | |
import requests | |
token = "{TELEGRAM TOKEN}" | |
mention_user = ["stypr", "stypr_jp"] | |
bugcamp_data = {} | |
def bugcamp(): | |
return requests.get("https://api.bugcamp.io/v1/programs").json() | |
def bugcamp_diff(a, b): | |
_a = [i['id'] for i in a['data']] | |
_b = [i['id'] for i in b['data']] | |
_add = set(_a) - set(_b) | |
_del = set(_b) - set(_a) | |
res_add = [] | |
res_del = [] | |
# add check | |
# if the difference is in a new data, it means the data exists. | |
for _data in a['data']: | |
if _data['id'] in _add: | |
res_add.append(_data) | |
return (res_add, res_del) | |
def start_timer(update, context): | |
user_id = update.effective_chat.id | |
context.bot.send_message(chat_id=user_id, text='Timer Started!') | |
context.job_queue.run_repeating(bugbounty, 3600, context=user_id) | |
def stop_timer(update, context): | |
user_id = update.effective_chat.id | |
context.bot.send_message(chat_id=user_id, text='Timer Stopped.') | |
context.job_queue.stop() | |
# check bugbounty | |
# possible bug: what if count suddenly changes | |
def bugbounty(context): | |
global bugcamp_data | |
global mention_user | |
user_id = context.job.context | |
# bugcamp search | |
bugcamp_data_curr = bugcamp() | |
# diff if count is wrong | |
result = "**bugcamp.io** - " | |
for username in mention_user: | |
result += f"[@{username}](mention:@{username})," | |
result = result[:-1] + "\n\n" | |
if bugcamp_data_curr['count'] != bugcamp_data['count']: | |
response = bugcamp_diff(bugcamp_data_curr, bugcamp_data) | |
result += "✅ *Added*\n" | |
if response[0]: | |
for data in response[0]: | |
result += f" - 제품명: {data['title']}\n" | |
result += f" - 요약: {data['summary']}\n" | |
result += f" - 최대 지급금액: {data['maximumReward']} KRW\n" | |
result += f" - 추가일: {data['createdAt']}\n" | |
result += f" - 진단대상: \n" | |
for _asset in data['assets']: | |
result += f" - {_asset['name']} ({_asset['type']}): {_asset['url']}\n" | |
result += "\n" | |
""" | |
result += "*Deleted*\n" | |
if response[1]: | |
for i in response[1]: | |
result += f" - Title: {data['title']}\n" | |
result += "\n" | |
""" | |
# send message and update | |
context.bot.send_message(chat_id=user_id, text=result, parse_mode=telegram.ParseMode.MARKDOWN) | |
bugcamp_data = bugcamp_data_curr | |
# else: | |
# context.bot.send_message(chat_id=user_id, text="pong", parse_mode=telegram.ParseMode.MARKDOWN) | |
# ping | |
def ping(update, context): | |
user_id = update.effective_chat.id | |
context.bot.send_message(chat_id=user_id, text='pong') | |
if __name__ == "__main__": | |
# init | |
bugcamp_data = bugcamp() | |
# bugcamp_data['count'] -= 1 | |
# bugcamp_data['data'].pop() | |
# updater | |
updater = Updater(token=token, use_context=True) | |
dispatcher = updater.dispatcher | |
# handler | |
start_handler = CommandHandler('bugbounty_start', start_timer, pass_job_queue=True) | |
stop_handler = CommandHandler('bugbounty_stop', stop_timer, pass_job_queue=True) | |
ping_handler = CommandHandler('ping', ping) | |
dispatcher.add_handler(start_handler) | |
dispatcher.add_handler(stop_handler) | |
dispatcher.add_handler(ping_handler) | |
# polling | |
updater.start_polling() | |
updater.idle() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment