-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patholdmain.py
65 lines (56 loc) · 2.27 KB
/
oldmain.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
import openai
import os
import telethon
from telethon.sync import TelegramClient
import json
import requests
api_id = os.environ.get("API_ID")
api_hash = os.environ.get("API_HASH")
bot_token = os.environ.get("BOT_TOKEN")
openai_api_key = os.environ.get("OPENAI_API_KEY")
telegraph_token = os.environ.get("TELEGRAPH_TOKEN")
client = TelegramClient(session='session_name', api_id=api_id, api_hash=api_hash)
client.start(bot_token=bot_token)
openai.api_key = openai_api_key
# /start command
@client.on(telethon.events.NewMessage(pattern='/start'))
async def start_handler(event):
await event.respond("Hello! I am a Telegram bot powered by the OpenAI ChatGPT model.\nTo use me send your questions along with /ask command.\n\n(c) Made by @HYBRID_BOTS")
# Handle all other messages
@client.on(telethon.events.NewMessage)
async def message_handler(event):
if event.message.message.strip().startswith("/ask"):
# Send message to notify user that response is being generated
generating_message = await event.reply("generating response...")
# Get the message text
message_text = event.message.message.strip().replace("/ask","",1).strip()
# Pass event object to callback function
response = openai.Completion.create(
engine="text-davinci-002",
prompt=f"{message_text}\n",
max_tokens=2048,
n=1,
stop=None,
temperature=0.5,
)
link = handle_response(event, response)
await event.reply(link)
await generating_message.delete()
def handle_response(event, response):
# Create a Telegraph article
headers = {
'Content-Type': 'application/json',
'Authorization': f'Token {telegraph_token}'
}
data = {
'access_token': telegraph_token,
'title': 'ChatGPT Response',
'content': [{'tag': 'p', 'children': [response.choices[0].text]}]
}
r = requests.post('https://api.telegra.ph/createPage', headers=headers, json=data)
r_json = r.json()
if r.status_code != 200 or not r_json['ok']:
return "Error creating telegraph page"
return f"https://telegra.ph/{r_json['result']['path']}"
# Run the bot
client.run_until_disconnected()