-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
107 lines (83 loc) · 2.91 KB
/
main.py
File metadata and controls
107 lines (83 loc) · 2.91 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
import requests
import json
from telegram import Update
from telegram.ext import Updater, MessageHandler, Filters
import time
import google.generativeai as genai
# Set up the Google Generative AI model
genai.configure(api_key="GEMINI API KEY")
generation_config = {
"temperature": 0.05,
"top_p": 1,
"top_k": 1,
"max_output_tokens": 10000,
}
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_ONLY_HIGH"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_ONLY_HIGH"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_ONLY_HIGH"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_ONLY_HIGH"
}
]
model = genai.GenerativeModel(model_name="gemini-pro",
generation_config=generation_config,
safety_settings=safety_settings)
# Telegram bot configuration
TOKEN = 'BOT TOKEN'
# Initialize last_request_time to 0
last_request_time = 0
def send_typing_animation(context, user_id):
context.bot.send_chat_action(chat_id=user_id, action="typing")
def send_spawning_animation(context, user_id):
# Add your spawning animation logic here
pass
def message(update: Update, context):
global last_request_time
user_id = update.effective_chat.id
message_text = update.message.text
headers = {
'Content-Type': 'application/json',
'x-goog-api-key': 'YOUR_GOOGLE_API_KEY'
}
try:
# Add typing animation
send_typing_animation(context, user_id)
# Check the time since the last request
elapsed_time = time.time() - last_request_time
# Implement rate limiting - wait if needed
if elapsed_time < 1.0: # Adjust this value based on your rate limits
time.sleep(1.0 - elapsed_time)
# Add spawning animation
send_spawning_animation(context, user_id)
# Use Google Generative AI for generating content
response = model.generate_content([message_text])
context.bot.send_message(chat_id=user_id, text="Bot: " + response.text)
except requests.exceptions.RequestException as e:
context.bot.send_message(chat_id=user_id, text="An error occurred while contacting the API.")
print("Request Exception:", e)
except Exception as e:
context.bot.send_message(chat_id=user_id, text="An error occurred while processing the response.")
print("Error:", e)
finally:
# Update the last request time
last_request_time = time.time()
def main():
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher
message_handler = MessageHandler(Filters.text & ~Filters.command, message)
dispatcher.add_handler(message_handler)
updater.start_polling()
updater.idle()
if __name__ == "__main__":
main()