-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbotapi.py
More file actions
195 lines (153 loc) · 6.54 KB
/
botapi.py
File metadata and controls
195 lines (153 loc) · 6.54 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from telegram import Update,InlineQueryResultArticle, InputTextMessageContent
from telegram.ext import Updater, CommandHandler, MessageHandler, InlineQueryHandler, CallbackContext, filters, CallbackQueryHandler
import database
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from uuid import uuid4
from config import botTOKEN
def start(update: Update, context: CallbackContext) -> None:
user = update.effective_user
update.message.reply_markdown_v2(f'''*Hi {user.mention_markdown_v2()}\!
I return JSON responses of both bot api and MTProto for your messages\.
Hit help to know more about how to use me\.
*''',
reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton(
"HELP", callback_data="getHELP"),
]]))
def help(update: Update, context: CallbackContext) -> None:
update.message.reply_markdown_v2(
f'''
Here is a detailed guide to use me\.
You can use me to get JSON responses of your messages\.
*Supports:*
\- `Messages`
\- `Inline Query`
\- `Callback Query`
Use /set to switch between `bot API` and `MTProto` mode and /button to generate sample inline keyboard buttons\.
''',
reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton("Get Help",
url="https://t.me/ostrichdiscussion"),
]]))
def button(update: Update, context: CallbackContext) -> None:
update.message.reply_markdown_v2(
f'''
*Sample Inline Buttons:*
''',
reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton("Button1", callback_data="button1"),
], [
InlineKeyboardButton("Button2", callback_data="Button2"),
]]))
def set_mode(update: Update, context: CallbackContext):
update.message.reply_markdown_v2(
text=f"*Select an option*",
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton("bot API", callback_data="set_botapi"),
], [
InlineKeyboardButton("MTProto", callback_data="set_mtproto"),
]]),
reply_to_message_id=update.message.message_id)
def new_message(update: Update, context: CallbackContext) -> None:
if update.message.chat.id == 1763185727:
return
update.message.reply_text(f"<code>{update}</code>", parse_mode="html")
def copy(update: Update, context: CallbackContext) -> None:
context.bot.copy_message(chat_id=update.message.chat.id,
from_chat_id=update.message.chat_id,
message_id=update.message.reply_to_message.message_id,
reply_markup=update.message.reply_to_message.reply_markup)
class ModeFilter(filters.UpdateFilter):
def filter(self, update: Update) -> bool:
keys = dir(update)
if "message" in keys:
user = update.message.chat.id
elif "inline_query" in keys:
user = update.inline_query["from"].id
else:
print(keys)
mode = database.find_mode(user)
return mode == 'botapi'
def inlinequery(update: Update, context: CallbackContext) -> None:
"""Handle the inline query."""
query = update.inline_query.query
results = [
InlineQueryResultArticle(
id=str(uuid4()),
title="Bot API response",
description="@responseJSONbot",
input_message_content=InputTextMessageContent(f"{update}")),
InlineQueryResultArticle(
id=str(uuid4()),
title="About",
description="@responseJSONbot",
url="https://t.me/theostrich",
input_message_content=InputTextMessageContent(f"{update}"),
),
]
update.inline_query.answer(results)
def callback(update: Update, context: CallbackContext) -> None:
if update.callback_query.message:
user = update.callback_query.message.chat.id
else:
user = update.callback_query["from"].id
mode = database.find_mode(update.callback_query.message.chat.id)
if mode == 'botapi':
query = update.callback_query
if query.data.startswith('set'):
query.answer()
mode = query.data.split("_")[1]
database.set_mode(user, mode)
query.message.reply_text(text=f"*Mode set to {mode} successfully*",
parse_mode="MARKDOWNV2")
elif query.data.startswith('getHELP'):
query.answer()
query.message.edit_text(
text=f'''
Here is a detailed guide to use me\.
You can use me to get JSON responses of your messages\.
*Supports:*
\- `Messages`
\- `Inline Query`
\- `Callback Query`
Use /set to switch between `bot API` and `MTProto` mode\.
''',
reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton("Get Help",
url="https://t.me/ostrichdiscussion"),
]]),
parse_mode="MARKDOWNV2",
disable_web_page_preview=True)
elif query.data == "closeInline":
query.message.delete_message()
else:
try:
context.bot.send_message(
chat_id=user,
text=f"<code>{update}</code>",
parse_mode='html',
disable_web_page_preview=True,
disable_notification=True,
)
except:
file = open("json.txt", "w+")
file.write(f"{update}")
file.close()
context.bot.send_document(chat_id=user,
document="json.txt",
caption="responseJSONbot",
disable_notification=True)
def main() -> None:
updater = Updater(botTOKEN)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", start, ModeFilter()))
dispatcher.add_handler(CommandHandler("help", help, ModeFilter()))
dispatcher.add_handler(CommandHandler("copy", copy, ModeFilter()))
dispatcher.add_handler(CommandHandler("button", button, ModeFilter()))
dispatcher.add_handler(CommandHandler("set", set_mode, ModeFilter()))
dispatcher.add_handler(CallbackQueryHandler(callback))
dispatcher.add_handler(InlineQueryHandler(inlinequery, ModeFilter()))
dispatcher.add_handler(MessageHandler(ModeFilter(), new_message))
updater.start_polling()
updater.idle()