11#!/usr/bin/env python3
22# coding=utf-8
33
4- from telegram .ext import Updater ,CommandHandler ,MessageHandler , Filters ,ConversationHandler ,InlineQueryHandler
5- from telegram import InlineQueryResultArticle , ParseMode ,InputTextMessageContent
4+ from telegram .ext import Updater , CommandHandler , MessageHandler , Filters , \
5+ ConversationHandler , InlineQueryHandler
6+ from telegram import InlineQueryResultArticle , InputTextMessageContent
67from uuid import uuid4
7- import requests ,json ,tempfile
8+ import requests
9+ from requests .packages .urllib3 .exceptions import InsecureRequestWarning
10+ import tempfile
11+
12+ requests .packages .urllib3 .disable_warnings (InsecureRequestWarning )
13+
14+
15+ # get token
16+ def tokenize (bot , job ):
17+ global headers
18+
19+ try :
20+ tokenRequest = requests .post (
21+ 'https://beepaste.io/api/v1/auth' ,
22+ headers = {'Content-Type' : 'application/json' }, verify = False )
23+ token_json = tokenRequest .json ()
24+ if token_json ['status' ] == 'success' :
25+ token = token_json ['X-TOKEN' ]
26+ headers = {'X-TOKEN' : token }
27+ except Exception as e :
28+ print ('\n Error: ' + e )
29+
830
931# main function , paste to beepaste.io
10- def paste (text ,author ):
11- # json data
12- data = {'api-key' : 'AAAAAAAAAAAAAAAAAAAAAAAAAAAA' , 'pasteRaw' : text , 'pasteLanguage' : 'text' , 'pasteTitle' : 'My Paste' , 'pasteAuthor' : author + ' , From @bpaste_bot ' }
13- # request and get url as json
14- rpj = requests .post ('https://beepaste.io/api' , json = data ).json ()
15- url = rpj ["url" ]
16- return url
32+ def paste (text , author ):
33+ data = {
34+ 'raw' : text ,
35+ 'title' : 'A new paste' ,
36+ 'author' : author + ' via @BPaste_bot 🤖'
37+ }
38+ try :
39+ r = requests .post (
40+ 'https://beepaste.io/api/v1/paste' ,
41+ headers = headers , json = data , verify = False )
42+ except Exception as e :
43+ print (e )
44+ rj = r .json ()
45+ if rj ['status' ] == 'success' :
46+ rjp = rj ['paste' ]
47+ return rjp
48+ else :
49+ return False
50+ tokenize ()
51+
1752
1853# access bot via token
1954updater = Updater (token = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' )
@@ -25,8 +60,9 @@ def start(bot, update):
2560 update .message .reply_text ('Hi, Give me your long text or send me your file' )
2661 return second
2762
63+
2864# get file and paste
29- def filef (bot ,update ):
65+ def filef (bot , update ):
3066 # get file id
3167 file = bot .getFile (update .message .document .file_id )
3268 # create a randon temp file name
@@ -35,109 +71,129 @@ def filef(bot,update):
3571 file .download (tf [1 ])
3672 # read file content
3773 try :
38- tfco = open (tf [1 ],"r" )
74+ tfco = open (tf [1 ], "r" )
3975 tfc = tfco .read ()
4076 tfco .close ()
4177 except Exception as e :
4278 # if it is not text file
43- bot .sendMessage (chat_id = update .message .chat_id ,reply_to_message_id = update .message .message_id ,text = " ⚠️ Please send me a text file" )
79+ bot .sendMessage (
80+ chat_id = update .message .chat_id ,
81+ reply_to_message_id = update .message .message_id ,
82+ text = " ⚠️ Please send me a text file" )
4483
4584 # get user name for author
4685 try :
47- author = update .message .from_user .first_name + " " + update .message .from_user .last_name
86+ author = update .message .from_user .first_name + " " \
87+ + update .message .from_user .last_name
4888 except Exception :
4989 author = "Anonymous user"
5090 # call paste function
51- url = paste (tfc , author )
91+ p = paste (tfc , author )
5292 # replay the url to user
53- bot .sendMessage (chat_id = update .message .chat_id ,reply_to_message_id = update .message .message_id ,text = url )
93+ bot .sendMessage (
94+ chat_id = update .message .chat_id ,
95+ reply_to_message_id = update .message .message_id ,
96+ text = '🔗 https://beta.beepaste.io/paste/view/' + p ['uri' ] + \
97+ '\n 🔗 ' + p ['shorturl' ]
98+ )
99+
54100
55101def second (bot , update ):
56102 # get text
57103 txt = update .message .text
58104 # get user name for author
59105 try :
60- author = update .message .from_user .first_name + " " + update .message .from_user .last_name
106+ author = update .message .from_user .first_name + " " + \
107+ update .message .from_user .last_name
61108 except Exception :
62109 author = "Anonymous user"
63110
64111 # call paste function
65- url = paste (txt , author )
112+ p = paste (txt , author )
66113 # replay the url to user
67- bot .sendMessage (chat_id = update .message .chat_id ,reply_to_message_id = update .message .message_id ,text = url )
114+ bot .sendMessage (
115+ chat_id = update .message .chat_id ,
116+ reply_to_message_id = update .message .message_id ,
117+ text = '🔗 https://beta.beepaste.io/paste/view/' + p ['uri' ] + \
118+ '\n 🔗 ' + p ['shorturl' ]
119+ )
120+
68121
69122# cancel function
70- def cancel (bot ,update ):
71- bot .sendMessage (chat_id = update .message .chat_id ,text = "canceled" )
123+ def cancel (bot , update ):
124+ bot .sendMessage (chat_id = update .message .chat_id , text = "canceled" )
125+
126+
72127# About function
73- def about (bot ,update ):
128+ def about (bot , update ):
74129 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 "
75- bot .sendMessage (chat_id = update .message .chat_id ,text = abouttext )
130+ bot .sendMessage (chat_id = update .message .chat_id , text = abouttext )
131+
76132
77133# inline respond function
78134def inlinequery (bot , update ):
79135 query = update .inline_query .query
80136 results = list ()
81- # get ans send inline info to paste function
82- def inlinepaste ():
83- global url
84- try :
85- author = update .inline_query .from_user .first_name + " " + update .inline_query .from_user .last_name
86- # set name for Anonymous user
87- except Exception :
88- author = "Anonymous user"
89- url = paste (query ,author )
90- return url
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+
91145 # add inline query to bot
92146 # inline mode for zero character
93- if len (query ) == 0 :
147+ if len (query ) == 0 :
94148 results .clear ()
95149 results .append (InlineQueryResultArticle (
96- id = uuid4 (),
97- title = " ▶️ Beepaste" ,
98- description = "type some text less than 256 character in inline mode" ,
99- url = "t.me/bpaste_bot" ,
100- thumb_url = "http://ip /beepastelogo.png" ,
101- input_message_content = InputTextMessageContent ( "Do you want to try me?! write something \n @bpaste_bot" )
102- ))
150+ 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" ) ))
103157 # inline mode for long text
104- elif len (query ) > 255 :
158+ elif len (query ) > 255 :
105159 results .clear ()
106160 results .append (InlineQueryResultArticle (
107- id = uuid4 (),
108- title = " Sorry! 😞" ,
109- description = " ⚠️ We can't get long long text in inline mode, send it directly" ,
110- url = "t.me/bpaste_bot" ,
111- thumb_url = "http://ip /beepastelogo.png" ,
112- input_message_content = InputTextMessageContent ( "We can't get so long text in inline mode, send it directly, @bpaste_bot" )
113- ))
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" ) ))
114168 # inline mode for normal text
115169 else :
116170 results .clear ()
171+ p = paste (query , author )
117172 results .append (InlineQueryResultArticle (
118- id = uuid4 (),
119- title = " ✅ Pasted!" ,
120- description = " pasted with this link" ,
121- url = inlinepaste (),
122- thumb_url = "http://ip/beepastelogo.png" ,
123- input_message_content = InputTextMessageContent (url )
124- ))
173+ id = uuid4 (),
174+ title = " ✅ Pasted!" ,
175+ description = " pasted with this link" ,
176+ url = p ['shorturl' ],
177+ thumb_url = "http://mostafaasadi.ir/bots/beepastelogo.png" ,
178+ input_message_content = InputTextMessageContent (
179+ '🔗 https://beta.beepaste.io/paste/view/' + p ['uri' ] + \
180+ '\n 🔗 ' + p ['shorturl' ])))
125181 # update inline respond
126182 update .inline_query .answer (results )
127183
184+
128185def main ():
129186 # manage conversation
130187 conv_handler = ConversationHandler (
131- entry_points = [CommandHandler ('start' , start )],
132- states = {
133- second : [MessageHandler (Filters .text ,second )],
134- },
135- fallbacks = [CommandHandler ('cancel' , cancel )])
188+ entry_points = [CommandHandler ('start' , start )],
189+ states = {
190+ second : [MessageHandler (Filters .text , second )]},
191+ fallbacks = [CommandHandler ('cancel' , cancel )])
136192
137193 # it handle start command
138194 start_handler = CommandHandler ('start' , start )
139195 # handle all text
140- second_handler = MessageHandler (Filters .text , second )
196+ second_handler = MessageHandler (Filters .text , second )
141197
142198 filef_handler = MessageHandler (Filters .document , filef )
143199 # handle cancel
@@ -153,11 +209,12 @@ def main():
153209 dispatcher .add_handler (cancel_handler )
154210 dispatcher .add_handler (about_handler )
155211 dispatcher .add_handler (filef_handler )
156-
212+ j = updater .job_queue
213+ j .run_repeating (tokenize , interval = 840 , first = 0 )
157214 # run
158215 updater .start_polling ()
159216 updater .idle ()
160- updater . stop ()
217+
161218
162219if __name__ == '__main__' :
163220 main ()
0 commit comments