Skip to content

Commit e2046a1

Browse files
committed
Implement replying to issues
1 parent e5f4c4e commit e2046a1

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

bot/github.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from bot.utils import github_cleaner, link, truncate, encode_data_link
1010

1111
TRUNCATED_MESSAGE = '\n<b>[Truncated message, open on GitHub to read more]</b>'
12-
REPLY_MESSAGE = '\n\n<i>Reply to this message to post a comment on GitHub.</i>'
12+
REPLY_MESSAGE = '\n\n<i>Reply to this message to post a comment on GitHub (use ! to suppress).</i>'
1313

1414

1515
def render_github_markdown(markdown, context: str):

bot/githubapi.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
GITHUB_OAUTH_CLIENT_SECRET, GITHUB_OAUTH_REDIRECT_URI)
1212
from bot.utils import secure_encode_64
1313

14+
GITHUB_API_ACCEPT = {'Accept': 'application/vnd.github.machine-man-preview+json'}
15+
1416

1517
class JWTAuth(AuthBase):
1618
def __init__(self, app_id):
@@ -150,5 +152,14 @@ def markdown(self, markdown, context):
150152

151153
return r.text
152154

155+
def add_issue_comment(self, repo, issue_number, body, access_token):
156+
r = self.post(f'https://api.github.com/repos/{repo}/issues/{issue_number}/comments', json={
157+
'body': body
158+
}, access_token=access_token)
159+
160+
r.raise_for_status()
161+
162+
return r.text
163+
153164

154165
github_api = GithubAPI()

bot/main.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import http.client
22
import logging
33

4-
from telegram import Update, ParseMode
5-
from telegram.ext import TypeHandler, CallbackContext, CommandHandler
4+
from telegram import Update, ParseMode, InlineKeyboardMarkup, InlineKeyboardButton
5+
from telegram.ext import TypeHandler, CallbackContext, CommandHandler, MessageHandler, Filters
66

77
from bot import settings
88
from bot.const import TELEGRAM_BOT_TOKEN, DATABASE_FILE
99
from bot.github import GithubHandler
10+
from bot.githubapi import github_api
1011
from bot.githubupdates import GithubUpdate, GithubAuthUpdate
1112
from bot.menu import reply_menu
1213
from bot.persistence import Persistence
1314
from bot.text import HELP_ADD_REPO
15+
from bot.utils import decode_first_data_entity, deep_link, reply_data_link_filter
1416
from bot.webhookupdater import WebhookUpdater
1517

1618
http.client.HTTPConnection.debuglevel = 5
@@ -61,6 +63,37 @@ def test_handler(update: Update, context: CallbackContext):
6163
pass
6264

6365

66+
def reply_handler(update: Update, context: CallbackContext):
67+
msg = update.effective_message
68+
69+
if msg.text[0] == '!':
70+
return
71+
72+
data = decode_first_data_entity(msg.reply_to_message.entities)
73+
74+
if not data:
75+
return
76+
77+
issue_type, repo, number, author = data
78+
79+
access_token = context.user_data.get('access_token')
80+
81+
if not access_token:
82+
msg.reply_text(f'Cannot reply to {issue_type}, since you are not logged in. '
83+
f'Press button below to go to a private chat with me and login.\n\n'
84+
f'<i>This message will self destruct in 30 sec.</i>',
85+
reply_markup=InlineKeyboardMarkup([[
86+
InlineKeyboardButton('Login', url=deep_link(context.bot, 'login'))
87+
]]),
88+
parse_mode=ParseMode.HTML)
89+
return
90+
91+
if issue_type == 'issue':
92+
text = f'@{author} {msg.text_markdown}'
93+
94+
github_api.add_issue_comment(repo, number, text, access_token=access_token)
95+
96+
6497
if __name__ == '__main__':
6598
persistence = Persistence(DATABASE_FILE)
6699
updater = WebhookUpdater(TELEGRAM_BOT_TOKEN,
@@ -78,6 +111,9 @@ def test_handler(update: Update, context: CallbackContext):
78111
dp.add_handler(CommandHandler('login', login_handler))
79112
dp.add_handler(CommandHandler('test', test_handler))
80113

114+
dp.add_handler(MessageHandler(Filters.reply & reply_data_link_filter, reply_handler,
115+
channel_post_updates=False, edited_updates=False))
116+
81117
settings.add_handlers(dp)
82118

83119
github_handler = GithubHandler(dp)

bot/utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
from html5lib.filters.base import Filter
1313
from html5lib.serializer import HTMLSerializer
1414
from telegram import MessageEntity
15+
from telegram.ext import BaseFilter
1516

1617
from bot.const import HMAC_SECRET
1718

18-
URL_BASE = 'https://a.test/'
19+
URL_BASE = 'https://ghbot.test/'
1920

2021

2122
class HMACException(Exception):
@@ -75,6 +76,21 @@ def decode_first_data_entity(entities):
7576
return decode_data_entity(entity)
7677

7778

79+
def deep_link(bot, data):
80+
return f'https://telegram.me/{bot.username}?start={data}'
81+
82+
83+
class _ReplyDataLinkFilter(BaseFilter):
84+
def filter(self, message):
85+
for entity in message.reply_to_message.entities:
86+
if entity.type == MessageEntity.TEXT_LINK:
87+
return entity.url.startswith(URL_BASE)
88+
break
89+
90+
91+
reply_data_link_filter = _ReplyDataLinkFilter()
92+
93+
7894
class _GithubFilter(Filter):
7995
def __iter__(self):
8096
in_quote = False

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy