Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telegrambot.py #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions README.md

This file was deleted.

29 changes: 29 additions & 0 deletions Telegrambot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import requests

def search_google(query):
api_key = "6569312893:AAFKp83PZWkhU-oc3ThOzOWs1QsgQTJ4qs4"
search_engine_id = "YOUR_SEARCH_ENGINE_ID"
url = f"https://www.googleapis.com/customsearch/v1?key={api_key}&cx={search_engine_id}&q={query}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
search_results = data.get("items", [])
return search_results
else:
return None

def handle_search(update, context):
query = " ".join(context.args)
search_results = search_google(query)
if search_results:
for result in search_results[:5]:
title = result.get("title")
link = result.get("link")
snippet = result.get("snippet")
message = f"<b>{title}</b>\n{snippet}\n<a href='{link}'>Read More</a>"
update.message.reply_html(message)
else:
update.message.reply_text("Sorry, I couldn't find any results for that query.")

# Add command handler for "/search" command
dispatcher.add_handler(CommandHandler("search", handle_search))