File size: 8,167 Bytes
ee6062f
c7dfe8b
 
 
 
76660cb
c7dfe8b
 
 
 
 
 
946b2f2
c7dfe8b
946b2f2
ee6062f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc17ccc
 
ee6062f
 
c7dfe8b
 
ee6062f
c7dfe8b
 
f8ea3ca
ee6062f
c7dfe8b
 
76660cb
 
 
 
 
 
 
 
 
f8ea3ca
76660cb
 
 
 
 
 
 
 
 
f8ea3ca
76660cb
 
 
 
 
 
ee6062f
f8ea3ca
 
 
c7dfe8b
 
 
 
 
 
 
 
 
ee6062f
 
 
c7dfe8b
ee6062f
c7dfe8b
 
ee6062f
c7dfe8b
 
 
 
 
ee6062f
 
c7dfe8b
 
 
 
 
 
 
 
 
 
 
ee6062f
c7dfe8b
ee6062f
c7dfe8b
ee6062f
c7dfe8b
ee6062f
 
 
c7dfe8b
ee6062f
 
 
 
 
c7dfe8b
ee6062f
 
 
 
c7dfe8b
 
 
 
 
 
946b2f2
 
c7dfe8b
946b2f2
 
 
 
c7dfe8b
946b2f2
 
c7dfe8b
946b2f2
 
 
 
 
c7dfe8b
946b2f2
 
 
 
 
c7dfe8b
946b2f2
 
c7dfe8b
946b2f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7dfe8b
 
 
 
 
 
 
 
 
 
 
 
ee6062f
c7dfe8b
ee6062f
c7dfe8b
ee6062f
c7dfe8b
ee6062f
c7dfe8b
 
ee6062f
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202


# <============================================== IMPORTS =========================================================>
import json
import random
import aiohttp

from pyrogram import Client, filters
from pyrogram.types import InputMediaPhoto, Message

from Mikobot import app
from Mikobot.state import state
import logging

logging.basicConfig(level=logging.DEBUG)
# <======================================== CONFIGURATION =========================================================>
# Centralized configuration for API settings
API_CONFIG = {
    "news_api": {
        "url": "https://newsapi.org/v2/everything?q={}&apiKey={api_key}",
        "api_key": "a66730fffb6b4d5c9a9db2a69ad1f9b5",
    },
    "bing_search_api": {
        "url": "https://api.bing.microsoft.com/v7.0/search?q={query}",
        "api_key": "YOUR_BING_SEARCH_API_KEY",
    },
    "bing_image_api": {
        "url": "https://api.bing.microsoft.com/v7.0/images/search?q={query}",
        "api_key": "YOUR_BING_IMAGE_API_KEY",
    },
    "google_image_api": {
        "url": "https://customsearch.googleapis.com/customsearch/v1?q={query}&searchType=image&key={api_key}",
        "api_key": "AIzaSyAwq2Vx4lyuXv6ZcZNjbws2v8Jhl0nAAmA",
        "cx": "a364d574f6cea4fc9",
    },
}

# <================================================ FUNCTION =======================================================>

@app.on_message(filters.command("news"))
async def news(_, message: Message):
    keyword = message.text.split(" ", 1)[1].strip() if len(message.text.split()) > 1 else ""
    api_url = API_CONFIG["news_api"]["url"].format(keyword, api_key=API_CONFIG["news_api"]["api_key"])

    try:
        async with aiohttp.ClientSession() as session:
            async with session.get(api_url) as response:
                print(f"Response Status Code: {response.status}")
                if response.status != 200:
                    await message.reply_text(f"API Error: {response.status}")
                    return
                
                news_data = await response.json()
                print(f"Response JSON: {news_data}")

                if news_data.get("status") == "ok":
                    articles = news_data.get("articles", [])
                    if articles:
                        news_item = random.choice(articles)
                        title = news_item.get("title", "No title")
                        excerpt = news_item.get("description", "No description")
                        source = news_item.get("source", {}).get("name", "Unknown source")
                        relative_time = news_item.get("publishedAt", "Unknown time")
                        news_url = news_item.get("url", "#")

                        message_text = f"π—§π—œπ—§π—Ÿπ—˜: {title}\n𝗦𝗒𝗨π—₯π—–π—˜: {source}\nπ—§π—œπ— π—˜: {relative_time}\nπ—˜π—«π—–π—˜π—₯𝗣𝗧: {excerpt}\n𝗨π—₯π—Ÿ: {news_url}"
                        await message.reply_text(message_text)
                    else:
                        await message.reply_text("No news found.")
                else:
                    await message.reply_text(f"API Response Error: {news_data.get('message', 'Unknown error')}")
    except Exception as e:
        print(f"Exception occurred: {e}")
        await message.reply_text(f"An error occurred: {str(e)}")



@app.on_message(filters.command("bingsearch"))
async def bing_search(client: Client, message: Message):
    try:
        if len(message.command) == 1:
            await message.reply_text("Please provide a keyword to search.")
            return

        keyword = " ".join(message.command[1:])
        api_url = API_CONFIG["bing_search_api"]["url"].format(query=keyword)
        headers = {"Ocp-Apim-Subscription-Key": API_CONFIG["bing_search_api"]["api_key"]}

        response = await state.get(api_url, headers=headers)

        if response.status_code == 200:
            results = response.json().get("webPages", {}).get("value", [])
            if not results:
                await message.reply_text("No results found.")
            else:
                message_text = ""
                for result in results[:7]:
                    title = result.get("name", "No title")
                    link = result.get("url", "No URL")
                    message_text += f"{title}\n{link}\n\n"
                await message.reply_text(message_text.strip())
        else:
            await message.reply_text("Sorry, something went wrong with the search.")
    except Exception as e:
        await message.reply_text(f"An error occurred: {str(e)}")


@app.on_message(filters.command("bingimg"))
async def bingimg_search(client: Client, message: Message):
    try:
        text = message.text.split(None, 1)[1]
    except IndexError:
        return await message.reply_text("Provide me a query to search!")

    search_message = await message.reply_text("πŸ”Ž")

    api_url = API_CONFIG["bing_image_api"]["url"].format(query=text)
    headers = {"Ocp-Apim-Subscription-Key": API_CONFIG["bing_image_api"]["api_key"]}
    response = await state.get(api_url, headers=headers)

    if response.status_code == 200:
        images = response.json().get("value", [])
        if not images:
            await message.reply_text("No images found.")
            return

        media = [InputMediaPhoto(media=img["contentUrl"]) for img in images[:7]]
        await message.reply_media_group(media=media)
    else:
        await message.reply_text("An error occurred while searching for images.")

    await search_message.delete()
    await message.delete()


@app.on_message(filters.command("googleimg"))
async def googleimg_search(_, message: Message):
    logging.debug("Google Image Search command invoked")
    try:
        query = message.text.split(None, 1)[1].strip() if len(message.text.split()) > 1 else None
        if not query:
            logging.debug("No query provided")
            return await message.reply_text("Please provide a search query!")

        logging.debug(f"Query: {query}")
        search_message = await message.reply_text("πŸ’­ Searching for images...")

        api_url = (
            f"https://www.googleapis.com/customsearch/v1"
            f"?q={query}&searchType=image&cx=YOUR_CSE_ID&key=YOUR_API_KEY&num=7"
        )
        logging.debug(f"API URL: {api_url}")

        async with aiohttp.ClientSession() as session:
            async with session.get(api_url) as response:
                logging.debug(f"API Response Status: {response.status}")
                if response.status != 200:
                    return await message.reply_text(f"Error: {response.status}")

                data = await response.json()
                logging.debug(f"API Response: {data}")

                items = data.get("items", [])
                if not items:
                    return await message.reply_text("No images found.")

                media = [InputMediaPhoto(item["link"]) for item in items[:7]]
                await message.reply_media_group(media=media)

        await search_message.delete()

    except IndexError:
        logging.exception("IndexError: No query provided")
        await message.reply_text("Provide me a query to search!")
    except Exception as e:
        logging.exception(f"Exception occurred: {str(e)}")
        await message.reply_text(f"An error occurred: {str(e)}")

# <=======================================================================================================>


# <=================================================== HELP ====================================================>
__mod_name__ = "SEARCH"

__help__ = """
πŸ’­ π—¦π—˜π—”π—₯𝗖𝗛

➠ *Available commands:*

Β» /googleimg <search query>: Retrieve and display images from Google Image Search.

Β» /bingimg <search query>: Retrieve and display images from Bing Image Search.

Β» /news <search query>: Search for news articles.

Β» /bingsearch <search query>: Perform a Bing web search and get results with links.

➠ *Example:*
➠ `/bingsearch app`: Returns search results.
"""
# <================================================ END =======================================================>