-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
261 lines (207 loc) · 8.61 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import textbase
from textbase.message import Message
from textbase import models
import os
from typing import List
from dotenv import load_dotenv
load_dotenv()
import openai
import requests
# API KEYS
MY_API_KEY = os.environ['MY_API_KEY']
WEATHER_API_KEY = os.environ['WEATHER_API']
URL_SHORTENER = os.environ['URL_SHORTENER']
TRANSLATE_API_KEY = os.environ['TRANSLATE_API_KEY']
HOLIDAYS_API_KEY = os.environ['HOLIDAYS_API_KEY']
MEME_API_KEY = os.environ['MEME_API_KEY']
JOKES_API_KEY = os.environ['JOKES_API_KEY']
SONGS_API_KEY = os.environ['SONGS_API_KEY']
# Load your OpenAI API key
models.OpenAI.api_key =MY_API_KEY
# or from environment variable:
# models.OpenAI.api_key = os.getenv("OPENAI_API_KEY")
# Prompt for GPT-3.5 Turbo
SYSTEM_PROMPT = """You are chatting with an AI. There are no specific prefixes for responses, so you can ask or talk about anything you like. The AI will respond in a natural, conversational manner. Feel free to start the conversation with any question or topic, and let's have a pleasant chat!
"""
MY_USERNAME = os.environ['MY_USERNAME']
CONTESTS_API_KEY = os.environ['CONTESTS_API_KEY']
def get_contest():
response = requests.get(f"https://clist.by/api/v1/contest/?username={MY_USERNAME}&api_key={CONTESTS_API_KEY}")
print(response);
return response;
def get_translate(text):
url = "https://google-translate1.p.rapidapi.com/language/translate/v2"
payload = {
"q": text,
"target": "hi",
"source": "en"
}
headers = {
"content-type": "application/x-www-form-urlencoded",
"Accept-Encoding": "application/gzip",
"X-RapidAPI-Key": TRANSLATE_API_KEY,
"X-RapidAPI-Host": "google-translate1.p.rapidapi.com"
}
response = requests.post(url, data=payload, headers=headers)
data = response.json();
translated_text = data['data']['translations'][0]['translatedText']
return translated_text
def get_urlshorten(long_url):
try:
url = "https://url-shortener-service.p.rapidapi.com/shorten"
payload = { "url":long_url }
headers = {
"content-type": "application/x-www-form-urlencoded",
"X-RapidAPI-Key": URL_SHORTENER,
"X-RapidAPI-Host": "url-shortener-service.p.rapidapi.com"
}
response = requests.post(url, data=payload, headers=headers)
res = response.json();
return "Here is your shorten URL: " + res["result_url"];
except :
return "Please re-enter your URL with along with http"
def get_memes():
url = "https://reddit-meme.p.rapidapi.com/memes/top"
headers = {
"X-RapidAPI-Key": MEME_API_KEY,
"X-RapidAPI-Host": "reddit-meme.p.rapidapi.com"
}
response = requests.get(url, headers=headers)
data = response.json()
title = data[0]['title']
url = data[0]['url']
return "<img src="+url+"/>\n"+title;
def get_joke():
url = "https://jokes-by-api-ninjas.p.rapidapi.com/v1/jokes"
headers = {
"X-RapidAPI-Key": JOKES_API_KEY,
"X-RapidAPI-Host": "jokes-by-api-ninjas.p.rapidapi.com"
}
response = requests.get(url, headers=headers).json()
data = response[0]['joke']
print(response)
return data
def download_song():
url = "https://spotify-downloader1.p.rapidapi.com/download/22LAwLoDA5b4AaGSkg6bKW"
headers = {
"X-RapidAPI-Key": SONGS_API_KEY,
"X-RapidAPI-Host": "spotify-downloader1.p.rapidapi.com"
}
response = requests.get(url, headers=headers)
data = response.json()
song_link = data['link']
image_url = data['metadata']['cover']
artists = data['metadata']['artists']
song_title = data['metadata']['title']
return f"""<strong>{song_title}</strong><br>
<strong>Artist: {artists}</strong>
<img src="{image_url}" alt="{song_title} cover" width="200">
<audio controls>
<source src="{song_link}" type="audio/mpeg">
Your browser does not support the audio element.
</audio>""";
def song_playlist():
url = "https://spotify-downloader1.p.rapidapi.com/trackList/playlist/37i9dQZF1DX0XUsuxWHRQd"
headers = {
"X-RapidAPI-Key": SONGS_API_KEY,
"X-RapidAPI-Host": "spotify-downloader1.p.rapidapi.com"
}
response = requests.get(url, headers=headers)
data = response.json()
print(data)
result = "<ul>"
for track in data['trackList']:
title = track['title']
artists = track['artists']
cover_url = track['cover']
album = track['album']
release_date = track['releaseDate']
result += f"""
<li>
<strong>Title:</strong> {title}<br>
<strong>Artists:</strong> {artists}<br>
<strong>Album:</strong> {album}<br>
<strong>Release Date:</strong> {release_date}<br>
<img src="{cover_url}" alt="{title} - {artists}" height="100" width="100">
</li>
"""
result += "</ul>"
return result
def get_holidays():
url = "https://working-days.p.rapidapi.com/1.3/analyse"
querystring = {"start_date":"2013-01-01","end_date":"2013-12-31","country_code":"IN","end_time":"18:15","start_time":"09:14"}
headers = {
"X-RapidAPI-Key": HOLIDAYS_API_KEY,
"X-RapidAPI-Host": "working-days.p.rapidapi.com"
}
response = requests.get(url, headers=headers, params=querystring)
data = response.json()
public_holidays = data.get('public_holidays', {}).get('list', [])
result_string = "<strong>Indian Public Holiday Days📍</strong>\n"
result_string += "<ul>\n---------------\n"
for holiday in public_holidays:
description = holiday.get('description', 'N/A')
date = holiday.get('date', 'N/A');
date_parts = date.split('-')
reversed_date_parts = date_parts[::-1]
reversed_date = '-'.join(reversed_date_parts)
result_string += f"<li><strong>{description}:</strong> <em>{reversed_date}</em></li>\n"
result_string += "</ul>"
return result_string
@textbase.chatbot("talking-bot")
def on_message(message_history: List[Message], state: dict = None):
"""Your chatbot logic here
message_history: List of user messages
state: A dictionary to store any stateful information
Return a string with the bot_response or a tuple of (bot_response: str, new_state: dict)
"""
if state is None or "counter" not in state:
state = {"counter": 0}
else:
state["counter"] += 1
# Get the latest user message
user_message = message_history[-1].content.lower()
if user_message.startswith("hi"):
bot_response = "Hello 😁! How can I assist you? Type start to look for all the commands"
elif user_message.startswith("start"):
bot_response = """
<p><strong>Here are some of the commands you can use:</strong></p>
<ul>
<li><strong>urlshorten [Link]:</strong> To get a shortened link for your long URL 😁</li>
<li><strong>translate [Text]:</strong> To translate your text from English to Hindi 💬</li>
<li><strong>holidays:</strong> To fetch all the public holidays of India 🎃</li>
<li><strong>ask:</strong> To ask any query from the internet 🧐</li>
<li><strong>meme:</strong> To generate a meme 😄</li>
<li><strong>joke:</strong> To generate a joke 😵💫</li>
<li><strong>playsong:</strong> To play a Spotify song 🎵</li>
<li><strong>spotifyplaylist:</strong> To get a Spotify playlist 📽️</li>
</ul>
"""
elif user_message.startswith("meme"):
bot_response=get_memes();
elif user_message.startswith("urlshorten"):
url = user_message.replace("urlshorten", "").strip()
print(url);
bot_response = get_urlshorten(url)
elif user_message.startswith("translate"):
text = user_message.replace("translate", "").strip().lower()
bot_response = get_translate(text)
elif user_message.startswith("holidays"):
bot_response = get_holidays()
elif user_message.startswith("generate"):
url = user_message.replace("generate", "").strip()
bot_response = generate_image(url)
elif user_message.startswith("joke"):
bot_response = get_joke()
elif user_message.startswith("playsong"):
bot_response = download_song()
elif user_message.startswith("spotifyplaylist"):
bot_response = song_playlist()
else:
# # Generate GPT-3.5 Turbo response
bot_response = models.OpenAI.generate(
system_prompt=SYSTEM_PROMPT,
message_history=user_message,
model="gpt-3.5-turbo",
)
return bot_response, state