Skip to content

Commit e43f57e

Browse files
committed
🗃 expand paste direct and inline
1 parent c707206 commit e43f57e

File tree

2 files changed

+125
-53
lines changed

2 files changed

+125
-53
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# [BPaste_bot](https://t.me/bpaste_bot)
22
### This is a telegram bot has been written with [python-telegram-bot](https://python-telegram-bot.org/) that paste long text in [BeePaste](https:beepaste.io)
33

4-
#### It can :
5-
- paste direct text
6-
- paste file content
7-
- paste inline mode
4+
# It can :
5+
- ## paste direct text
6+
- ## paste file content
7+
- ## paste inline mode
8+
- ## expand a paste content via link

bp.py

Lines changed: 120 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22
# coding=utf-8
33

44
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, \
5-
ConversationHandler, InlineQueryHandler
6-
from telegram import InlineQueryResultArticle, InputTextMessageContent
5+
ConversationHandler, InlineQueryHandler, RegexHandler
6+
from telegram import InlineQueryResultArticle, InputTextMessageContent, \
7+
MessageEntity, ParseMode
8+
from requests.packages.urllib3.exceptions import InsecureRequestWarning
9+
import re
710
from uuid import uuid4
811
import requests
9-
from requests.packages.urllib3.exceptions import InsecureRequestWarning
1012
import tempfile
1113

1214
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
1315

16+
# access bot via token
17+
updater = Updater(token='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
18+
dispatcher = updater.dispatcher
19+
second = 2
20+
1421

1522
# get token
1623
def tokenize(bot, job):
@@ -50,10 +57,46 @@ def paste(text, author):
5057
tokenize()
5158

5259

53-
# access bot via token
54-
updater = Updater(token='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
55-
dispatcher = updater.dispatcher
56-
second = 2
60+
# expand paste
61+
def expand(link):
62+
pid = link.split('/')[5].split(' ')[0]
63+
64+
try:
65+
r = requests.get(
66+
'https://beepaste.io/api/v1/paste/' + pid,
67+
headers=headers
68+
)
69+
except Exception as e:
70+
print(e)
71+
rj = r.json()
72+
if rj['status'] == 'success':
73+
return rj['paste']
74+
else:
75+
return False
76+
tokenize()
77+
78+
79+
# expand paste in direct
80+
def expand_direct(bot, update):
81+
try:
82+
p = expand(update.message.text)
83+
res = '🔗 https://beepaste.io/paste/view/' + p['uri'] + \
84+
'\n👤 ' + p['author'].replace('_', '\\_') + \
85+
'\n```\n\n' + p['raw'] + '\n```'
86+
bot.sendMessage(
87+
chat_id=update.message.chat_id,
88+
parse_mode=ParseMode.MARKDOWN,
89+
disable_web_page_preview=True,
90+
reply_to_message_id=update.message.message_id,
91+
text=res
92+
)
93+
except Exception as e:
94+
bot.sendMessage(
95+
chat_id=update.message.chat_id,
96+
reply_to_message_id=update.message.message_id,
97+
text='❌'
98+
)
99+
57100

58101
# start_handler function
59102
def start(bot, update):
@@ -126,59 +169,75 @@ def cancel(bot, update):
126169

127170
# About function
128171
def about(bot, update):
129-
abouttext = "@BPaste_bot , Can Paste Long Text and File Content In Groups and Private Chats , Via inline Mode Or Directly \n🆓 This Bot , Totaly Free , Libre and OpenSource \n 🌐 https://github.com/mostafaasadi/bpastebot "
172+
abouttext = "@BPaste_bot , Can Paste Long Text and File Content In Groups and Private Chats , Via inline Mode Or Directly or Expand your Paste \n🆓 This Bot , Totaly Free , Libre and OpenSource \n 🌐 https://github.com/mostafaasadi/bpastebot "
130173
bot.sendMessage(chat_id=update.message.chat_id, text=abouttext)
131174

132175

133176
# inline respond function
134177
def inlinequery(bot, update):
135178
query = update.inline_query.query
136179
results = list()
137-
138-
try:
139-
author = update.inline_query.from_user.first_name + " " + \
140-
update.inline_query.from_user.last_name
141-
# set name for Anonymous user
142-
except Exception:
143-
author = "Anonymous user"
144-
145-
# add inline query to bot
146-
# inline mode for zero character
147-
if len(query) == 0:
148-
results.clear()
180+
# beepaste link to expand
181+
if re.match('https://beepaste.io/paste/view\S+', query):
182+
p = expand(query)
183+
res = '🔗 https://beepaste.io/paste/view/' + p['uri'] + \
184+
'\n👤 ' + p['author'].replace('_', '\\_') + \
185+
'\n```\n\n' + p['raw'] + '\n```'
149186
results.append(InlineQueryResultArticle(
150187
id=uuid4(),
151-
title=" ▶️ Beepaste",
152-
description="type some text less than 256 character inline mode",
153-
url="t.me/bpaste_bot",
154-
thumb_url="http://mostafaasadi.ir/bots/beepastelogo.png",
155-
input_message_content=InputTextMessageContent(
156-
"BPaste, paste your world! \n@bpaste_bot")))
157-
# inline mode for long text
158-
elif len(query) > 255:
159-
results.clear()
160-
results.append(InlineQueryResultArticle(
161-
id=uuid4(),
162-
title=" Sorry! 😞",
163-
description=" ⚠️ We can't get long long text in inline mode, send it directly",
164-
url="t.me/bpaste_bot",
165-
thumb_url="http://mostafaasadi.ir/bots/beepastelogo.png",
166-
input_message_content=InputTextMessageContent(
167-
"We can't get so long text in inline mode, send it directly, @bpaste_bot")))
168-
# inline mode for normal text
169-
else:
170-
results.clear()
171-
p = paste(query, author)
172-
results.append(InlineQueryResultArticle(
173-
id=uuid4(),
174-
title=" ✅ Pasted!",
175-
description=" pasted with this link",
188+
title=" ✅ Expanded!",
189+
description=p['author'].replace('_', '\\_'),
176190
url=p['shorturl'],
177191
thumb_url="http://mostafaasadi.ir/bots/beepastelogo.png",
178192
input_message_content=InputTextMessageContent(
179-
'🔗 https://beepaste.io/paste/view/' + p['uri'] + \
180-
'\n🔗 ' + p['shorturl'])))
181-
# update inline respond
193+
res,
194+
disable_web_page_preview=True,
195+
parse_mode=ParseMode.MARKDOWN)))
196+
else:
197+
try:
198+
author = update.inline_query.from_user.first_name + " " + \
199+
update.inline_query.from_user.last_name
200+
# set name for Anonymous user
201+
except Exception:
202+
author = "Anonymous user"
203+
204+
# add inline query to bot
205+
# inline mode for zero character
206+
if len(query) == 0:
207+
results.clear()
208+
results.append(InlineQueryResultArticle(
209+
id=uuid4(),
210+
title=" ▶️ BPaste",
211+
description="type some text less than 256 character inline mode or a paste link to expand",
212+
url="t.me/bpaste_bot",
213+
thumb_url="http://mostafaasadi.ir/bots/beepastelogo.png",
214+
input_message_content=InputTextMessageContent(
215+
"BPaste, paste your world! \n@bpaste_bot")))
216+
# inline mode for long text
217+
elif len(query) > 255:
218+
results.clear()
219+
results.append(InlineQueryResultArticle(
220+
id=uuid4(),
221+
title=" Sorry! 😞",
222+
description=" ⚠️ We can't get long long text in inline mode, send it directly",
223+
url="t.me/bpaste_bot",
224+
thumb_url="http://mostafaasadi.ir/bots/beepastelogo.png",
225+
input_message_content=InputTextMessageContent(
226+
"We can't get so long text in inline mode, send it directly, @bpaste_bot")))
227+
# inline mode for normal text
228+
else:
229+
results.clear()
230+
p = paste(query, author)
231+
results.append(InlineQueryResultArticle(
232+
id=uuid4(),
233+
title=" ✅ Pasted!",
234+
description=" pasted with this link",
235+
url=p['shorturl'],
236+
thumb_url="http://mostafaasadi.ir/bots/beepastelogo.png",
237+
input_message_content=InputTextMessageContent(
238+
'🔗 https://beepaste.io/paste/view/' + p['uri'] + \
239+
'\n🔗 ' + p['shorturl'])))
240+
# update inline respond
182241
update.inline_query.answer(results)
183242

184243

@@ -193,7 +252,11 @@ def main():
193252
# it handle start command
194253
start_handler = CommandHandler('start', start)
195254
# handle all text
196-
second_handler = MessageHandler(Filters.text, second)
255+
second_handler = MessageHandler(
256+
Filters.text | Filters.entity(MessageEntity.TEXT_LINK) &
257+
~ Filters.entity(MessageEntity.URL),
258+
second
259+
)
197260

198261
filef_handler = MessageHandler(Filters.document, filef)
199262
# handle cancel
@@ -204,11 +267,19 @@ def main():
204267
# handle dispatcher
205268
dispatcher.add_handler(InlineQueryHandler(inlinequery))
206269
dispatcher.add_handler(start_handler)
270+
# handle beepaste link for expand
271+
dispatcher.add_handler(
272+
RegexHandler(
273+
'https://beepaste.io/paste/view\S+',
274+
expand_direct
275+
)
276+
)
207277
dispatcher.add_handler(second_handler)
208278
dispatcher.add_handler(conv_handler)
209279
dispatcher.add_handler(cancel_handler)
210280
dispatcher.add_handler(about_handler)
211281
dispatcher.add_handler(filef_handler)
282+
212283
j = updater.job_queue
213284
j.run_repeating(tokenize, interval=840, first=0)
214285
# run

0 commit comments

Comments
 (0)