forked from mostafaasadi/bpastebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbp.py
executable file
·97 lines (86 loc) · 3.41 KB
/
bp.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
#!/usr/bin/env python3
# coding=utf-8
from telegram.ext import Updater,CommandHandler,MessageHandler, Filters ,ConversationHandler,InlineQueryHandler
from telegram import InlineQueryResultArticle, ParseMode,InputTextMessageContent
from uuid import uuid4
import requests,json
# main function , paste to beepaste.io
def paste(text,author):
# json data
data = {'api-key': 'AAAAAAAAAAAAAAAAAAAA', 'pasteRaw': text, 'pasteLanguage': 'text', 'pasteTitle': 'My Paste', 'pasteAuthor': author + ' , From @bpaste_bot '}
# request
rp = requests.post('https://beepaste.io/api', json=data)
# get url as json
rpj = rp.json()
url = rpj["url"]
return url
# access bot via token
updater = Updater(token='NNNNNNNNN:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
dispatcher = updater.dispatcher
second = 2
# start_handler function
def start(bot, update):
update.message.reply_text('Hi, Give me your long text')
return second
# get text and call paste function
def second(bot, update):
# get text
txt = update.message.text
# get user name for author
try:
author = update.message.from_user.first_name + " " + update.message.from_user.last_name
except Exception:
author = "Anonymous user"
# call paste function
url = paste(txt , author)
# replay the url to user
bot.sendMessage(chat_id=update.message.chat_id,reply_to_message_id=update.message.message_id,text=url)
# cancel function
def cancel(bot,update):
bot.sendMessage(chat_id=update.message.chat_id,text="canceled")
# About function
def about(bot,update):
abouttext = " @bpaste_bot is a Free Software that developed by Mostafa Asadi \n\n 🌐 https://github.com/mostafaasadi/bpastebot "
bot.sendMessage(chat_id=update.message.chat_id,text=abouttext)
# inline respond function
def inlinequery(bot, update):
query = update.inline_query.query
results = list()
# get ans send inline info to paste function
def inlinepaste():
try:
author = update.inline_query.from_user.first_name + " " + update.inline_query.from_user.last_name
except Exception:
author = "Anonymous user"
url = paste(query,author)
return url
# add inline query to bot
results.append(InlineQueryResultArticle(id=uuid4(),title="Paste it!" , description="put your text and click", thumb_url="http://url/beepastelogo.png", input_message_content=InputTextMessageContent(inlinepaste())))
# update inline respond
update.inline_query.answer(results)
# manage conversation
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
second: [MessageHandler(Filters.text,second)],
},
fallbacks=[CommandHandler('cancel', cancel)])
# it handle start command
start_handler = CommandHandler('start', start)
# handle all text
second_handler = MessageHandler(Filters.text , second)
# handle cancel
cancel_handler = CommandHandler('cancel', cancel)
# handle cancel
about_handler = CommandHandler('about', about)
# handle dispatcher
dispatcher.add_handler(InlineQueryHandler(inlinequery))
dispatcher.add_handler(start_handler)
dispatcher.add_handler(second_handler)
dispatcher.add_handler(conv_handler)
dispatcher.add_handler(cancel_handler)
dispatcher.add_handler(about_handler)
# run
updater.start_polling()
updater.idle()
updater.stop()