-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.rb
More file actions
53 lines (41 loc) · 1.2 KB
/
Copy pathbot.rb
File metadata and controls
53 lines (41 loc) · 1.2 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
require 'dotenv/load'
require 'telegram/bot'
require 'logger'
require_relative 'client'
token = ENV.fetch('TELEGRAM_BOT_TOKEN')
$telegram_user_ids = ENV.fetch('TELEGRAM_USER_IDS', '').split(',')
def client
@client ||= Client.new
end
def logger
@logger ||= Logger.new($stdout)
end
def ask_chatgpt(messages, user_message)
return "Ask me anything you want" if user_message.text == '/start'
response = client.chat(messages)
return "Service Temporarily Unavailable" unless response.success?
response.body[:choices][0][:message][:content]
rescue StandardError => e
logger.error e
"Service Temporarily Unavailable"
end
def handle_message(bot, message)
# return if message != Telegram::Bot::Types::Message
return "Access denied" if !$telegram_user_ids.empty? && !$telegram_user_ids.include?(message.from&.id.to_s)
# Get the user's input text
input_text = message.text
messages = [
{ role: "user", content: input_text }
]
answer = ask_chatgpt(messages, message)
# Send the response back to the user
bot.api.send_message(
chat_id: message.chat.id,
text: answer
)
end
Telegram::Bot::Client.run(token) do |bot|
bot.listen do |message|
handle_message(bot, message)
end
end