File size: 5,754 Bytes
c7dfe8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SOURCE https://github.com/Team-ProjectCodeX
# CREATED BY https://t.me/O_okarma
# PROVIDED BY https://t.me/ProjectCodeX
# βž₯ @MIKO_V2BOT Κα΄α΄œΚ€ ᴍᴇssᴀɒᴇ @ᴜsᴇʀɴᴀᴍᴇ
# βž₯ @MIKO_V2BOT @ᴜsᴇʀɴᴀᴍᴇ Κα΄α΄œΚ€ ᴍᴇssᴀɒᴇ

# TURN ON INLINE MODE FOR USE.

# <============================================== IMPORTS =========================================================>
import shortuuid
from pymongo import MongoClient
from telegram import (
    InlineKeyboardButton,
    InlineKeyboardMarkup,
    InlineQueryResultArticle,
    InputTextMessageContent,
    Update,
)
from telegram.ext import CallbackQueryHandler, ContextTypes, InlineQueryHandler

from Mikobot import DB_NAME, MONGO_DB_URI, function

# Initialize MongoDB client
client = MongoClient(MONGO_DB_URI)
db = client[DB_NAME]
collection = db["whispers"]


# <==================================================== CLASS ===================================================>
# Whispers Class
class Whispers:
    @staticmethod
    def add_whisper(WhisperId, WhisperData):
        whisper = {"WhisperId": WhisperId, "whisperData": WhisperData}
        collection.insert_one(whisper)

    @staticmethod
    def del_whisper(WhisperId):
        collection.delete_one({"WhisperId": WhisperId})

    @staticmethod
    def get_whisper(WhisperId):
        whisper = collection.find_one({"WhisperId": WhisperId})
        return whisper["whisperData"] if whisper else None


# <==================================================== BOOT FUNCTION ===================================================>
# Inline query handler
async def mainwhisper(update: Update, context: ContextTypes.DEFAULT_TYPE):
    query = update.inline_query
    if not query.query:
        return await query.answer(
            [],
            switch_pm_text="Give me a username or ID!",
            switch_pm_parameter="ghelp_whisper",
        )

    user, message = parse_user_message(query.query)
    if len(message) > 200:
        return

    usertype = "username" if user.startswith("@") else "id"

    if user.isdigit():
        try:
            chat = await context.bot.get_chat(int(user))
            user = f"@{chat.username}" if chat.username else chat.first_name
        except Exception:
            pass

    whisperData = {
        "user": query.from_user.id,
        "withuser": user,
        "usertype": usertype,
        "type": "inline",
        "message": message,
    }
    whisperId = shortuuid.uuid()

    # Add the whisper to the database
    Whispers.add_whisper(whisperId, whisperData)

    answers = [
        InlineQueryResultArticle(
            id=whisperId,
            title=f"πŸ‘€ Send a whisper message to {user}!",
            description="Only they can see it!",
            input_message_content=InputTextMessageContent(
                f"πŸ” A Whisper Message For {user}\nOnly they can see it!"
            ),
            reply_markup=InlineKeyboardMarkup(
                [
                    [
                        InlineKeyboardButton(
                            "πŸ“© π—¦π—΅π—Όπ˜„ π—ͺπ—΅π—Άπ˜€π—½π—²π—Ώ πŸ“©",
                            callback_data=f"whisper_{whisperId}",
                        )
                    ]
                ]
            ),
        )
    ]

    await context.bot.answer_inline_query(query.id, answers)


# Callback query handler
async def showWhisper(update: Update, context: ContextTypes.DEFAULT_TYPE):
    callback_query = update.callback_query
    whisperId = callback_query.data.split("_")[-1]
    whisper = Whispers.get_whisper(whisperId)

    if not whisper:
        await context.bot.answer_callback_query(
            callback_query.id, "This whisper is not valid anymore!"
        )
        return

    userType = whisper["usertype"]
    from_user_id = callback_query.from_user.id

    if from_user_id == whisper["user"]:
        await context.bot.answer_callback_query(
            callback_query.id, whisper["message"], show_alert=True
        )
    elif (
        userType == "username"
        and callback_query.from_user.username
        and callback_query.from_user.username.lower()
        == whisper["withuser"].replace("@", "").lower()
    ):
        await context.bot.answer_callback_query(
            callback_query.id, whisper["message"], show_alert=True
        )
    elif userType == "id" and from_user_id == int(whisper["withuser"]):
        await context.bot.answer_callback_query(
            callback_query.id, whisper["message"], show_alert=True
        )
    else:
        await context.bot.answer_callback_query(
            callback_query.id, "Not your Whisper!", show_alert=True
        )


# Function to parse user message
def parse_user_message(query_text):
    text = query_text.split(" ")
    user = text[0]
    first = True
    message = ""

    if not user.startswith("@") and not user.isdigit():
        user = text[-1]
        first = False

    if first:
        message = " ".join(text[1:])
    else:
        text.pop()
        message = " ".join(text)

    return user, message


# <==================================================== FUNCTION ===================================================>
# Add handlers
function(InlineQueryHandler(mainwhisper, block=False))
function(CallbackQueryHandler(showWhisper, pattern="^whisper_", block=False))


# <==================================================== HELP ===================================================>
__help__ = """
➠ *Whisper inline function for secret chats.*

➠ *Commands:*

Β» @MIKO_V2BOT your message @username
Β» @MIKO_V2BOT @username your message
"""

__mod_name__ = "WHISPER-MSG"
# <==================================================== END ===================================================>