-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
145 lines (123 loc) · 4.31 KB
/
main.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
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
import asyncio
import base64
import functools
import os
import traceback
from io import BytesIO
import requests
from PIL import Image
from dotenv import load_dotenv
from telethon import TelegramClient, events
load_dotenv()
# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = os.environ["API_ID"]
bot_token = os.environ["BOT_TOKEN"]
api_hash = os.environ["API_HASH"]
rapidapi_key = os.environ["RAPIDAPI_KEY"]
bot_name = os.environ["BOT_NAME"]
client = TelegramClient('bot_session', api_id, api_hash).start(bot_token=bot_token)
def save_img(user_id, img_url):
resp = requests.get(img_url, stream=True)
image_name = f"temp_{user_id}.jpg"
image_path = f"./temp/{image_name}"
with open(image_path, "wb") as f:
f.write(resp.content)
return image_path
def convert_to_base64(image_path):
image = Image.open(image_path)
buffered = BytesIO()
image.save(buffered, format="PNG")
b64_img = base64.b64encode(buffered.getvalue()).decode("utf-8")
return b64_img
def call_machaao_rapidapi(user_id, text, image_str):
output = {
"text": None,
"image": None
}
url = f"https://messengerx-io.p.rapidapi.com/process/{bot_name}"
payload = {
"message_data": {
"text": f"{text}"
}
}
if image_str:
payload["message_data"]["attachment"] = {
"type": "image/jpeg",
"raw": f"data:image/jpeg;base64,{image_str}"
}
headers = {
"x-rapidapi-key": f"{rapidapi_key}",
"x-rapidapi-host": "messengerx-io.p.rapidapi.com",
"Content-Type": "application/json",
"X-Sender-Id": f"{user_id}"
}
response = requests.post(url, json=payload, headers=headers)
resp = response.json()
_output = resp["output"]
text = _output["output"]
attachment = _output.get("attachment", None)
if attachment:
output["image"] = attachment.get("url")
output["text"] = attachment.get("text")
# url_prefix = "https://ganglia.machaao.com/download"
# if url_prefix in text:
# url_start = text.find(url_prefix)
# url = text[url_start:]
# text = text[:url_start]
# output["image"] = url
# output["text"] = text
else:
output["text"] = text
return output
save_path = "downloads/"
# Define the main chatbot handler
@client.on(events.NewMessage())
async def handle_start_command(event):
SENDER = await event.get_sender()
try:
base64_image = None
if event.photo:
if event.message.message:
user_input = event.message.message
else:
user_input = "Describe this image"
saved_path = await event.download_media(save_path)
base64_image = convert_to_base64(saved_path)
else:
user_input = event.message.message
# user_id, text, image_url
print(f"{SENDER.id}-> Message: {user_input}")
history = [{"role": "user", "content": user_input}]
loop = asyncio.get_running_loop()
async with client.action(event.chat_id, 'typing'):
resp = await loop.run_in_executor(
None,
functools.partial(
call_machaao_rapidapi,
user_id=SENDER.id,
text=user_input,
image_str=base64_image
)
)
if resp.get("image") is None:
text = resp["text"]
history.append({"role": "assistant", "content": text})
await client.send_message(SENDER, text, parse_mode='Markdown')
else:
image_url = resp["image"]
text = resp["text"]
img_path = save_img(SENDER.id, image_url)
await client.send_file(SENDER, file=img_path, caption=text)
os.remove(img_path)
except Exception as e:
print(traceback.format_exc())
error_msg = "Something went wrong. Please try again."
await client.send_message(SENDER, error_msg, parse_mode='Markdown')
return
# Main function
if __name__ == "__main__":
os.makedirs("temp", exist_ok=True)
os.makedirs("downloads", exist_ok=True)
print("Bot Started...")
client.run_until_disconnected() # Start the bot!