-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatBot.py
70 lines (52 loc) · 1.76 KB
/
chatBot.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
# -*- coding: utf-8 -*-
"""
Documentation:
"""
# ---------------------------------
# Import Libraries
import requests
from aiogram import Bot, Dispatcher, types
from aiogram.utils import executor
# ---------------------------------
# Variables
# ---------------------------------
# Start Here
def create_bot(telegram_bot_token, rapid_api_token):
bot = Bot(token=telegram_bot_token)
dp = Dispatcher(bot)
@dp.message_handler(commands=['start'])
async def start_command(message: types.Message):
await message.reply("Hi! I'm a bot created by Michael Reda.")
@dp.message_handler()
async def echo_message(message: types.Message):
msg_text = message.text
msg_id = message.chat.id
await bot.send_chat_action(chat_id=msg_id, action=types.ChatActions.TYPING)
ai_response = post_request(msg_text, rapid_api_token)
await message.reply(ai_response)
executor.start_polling(dp, skip_updates=True)
def post_request(in_text: str, rapidapi_key: str, model: str = "gpt-3.5-turbo"):
url = "https://openai80.p.rapidapi.com/chat/completions"
payload = {
"model": model,
"messages": [
{
"role": "user",
"content": in_text
}
]
}
headers = {
"content-type": "application/json",
"X-RapidAPI-Key": rapidapi_key,
"X-RapidAPI-Host": "openai80.p.rapidapi.com"
}
response = requests.request("POST", url, json=payload, headers=headers)
return response.json().get('choices')[0].get('message').get('content')
# Main Function
def main():
telegram_bot_token = "TELEGRAM_TOKEN"
rapidapi_token = "RAPIDAPI_TOKEN"
create_bot(telegram_bot_token, rapidapi_token)
if __name__ == '__main__':
main()