-
Notifications
You must be signed in to change notification settings - Fork 11
/
botai.py
53 lines (38 loc) · 1.56 KB
/
botai.py
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
# This scripts contains use cases for simple bots
import google.generativeai as genai
from pyrogram import Client, filters, enums
from pyrogram.types import Message
API_KEY="your gemini api key here"
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel("gemini-pro")
API_ID = your_api_id
API_HASH = 'your_api_hash'
BOT_TOKEN = 'your_bot_token'
app = Client("gemini_ai", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN)
@app.on_message(filters.command("start") & filters.private)
async def start(_, message):
chat = model.start_chat()
prompt = "Hi"
response = chat.send_message(prompt)
await message.reply_text(f"{response.text}", parse_mode=enums.ParseMode.MARKDOWN)
@app.on_message(filters.command("ask") & filters.private)
async def say(_, message: Message):
try:
i = await message.reply_text("<code>Please Wait...</code>")
if len(message.command) > 1:
prompt = message.text.split(maxsplit=1)[1]
elif message.reply_to_message:
prompt = message.reply_to_message.text
else:
await message.reply_text(
f"<b>Usage: </b><code>/ask [prompt/reply to message]</code>"
)
return
chat = model.start_chat()
response = chat.send_message(prompt)
i.delete()
await message.reply_text(f"**Question:**`{prompt}`\n**Answer:** {response.text}", parse_mode=enums.ParseMode.MARKDOWN)
except Exception as e:
await message.reply_text(f"An error occurred: {str(e)}")
if __name__ == "__main__":
app.run()