Skip to content
This repository has been archived by the owner on Jan 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #21 from JesusGautamah/v0.1.4
Browse files Browse the repository at this point in the history
Changed vistor model and migration, now other users can use the bot too
  • Loading branch information
JesusGautamah committed Apr 10, 2023
2 parents a71f7ef + 4b2533f commit 279a9cb
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 40 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
AllCops:
TargetRubyVersion: 2.6
NewCops: enable

Style/StringLiterals:
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions chatgpt_assistant.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ Gem::Specification.new do |spec|
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.metadata["rubygems_mfa_required"] = "true"
end
2 changes: 1 addition & 1 deletion exe/chatgpt_bot
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require_relative "../lib/chatgpt_assistant"

puts "Starting Chatgpt Assistant"
puts "Bot Type: #{ARGV[0]}"
puts "Mode: #{ENV["MODE"]}"
puts "Mode: #{ENV.fetch("MODE", nil)}"
case ARGV[0]
when "telegram"
ChatgptAssistant::Main.new("telegram").start
Expand Down
12 changes: 6 additions & 6 deletions lib/bots/discord_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ def start_event

def login_event
bot.command :login do |event|
@message = event.message.content.split(" ")[1]
@message = event.message.content.split[1]
@evnt = event
message.nil? ? event.respond(commom_messages[:login]) : login_action
end
end

def register_event
bot.command :register do |event|
@message = event.message.content.split(" ")[1]
@message = event.message.content.split[1]
@evnt = event
message.nil? ? event.respond(commom_messages[:register]) : register_action
end
Expand All @@ -68,7 +68,7 @@ def hist_event
bot.command :hist do |event|
@evnt = event
event.respond error_messages[:user_not_logged_in] if user.nil?
title = event.message.content.split(" ")[1..].join(" ")
title = event.message.content.split[1..].join(" ")
@chat = user.chat_by_title(title)
event.respond error_messages[:chat_not_found] if chat.nil? && user
hist_action if user && chat
Expand All @@ -94,7 +94,7 @@ def new_chat_event
def sl_chat_event
bot.command :sl_chat do |event|
@evnt = event
chat_to_select = event.message.content.split(" ")[1..].join(" ")
chat_to_select = event.message.content.split[1..].join(" ")
@user = find_user(discord_id: event.user.id)
event.respond error_messages[:user_not_logged_in] if user.nil?

Expand All @@ -105,7 +105,7 @@ def sl_chat_event
def ask_event
bot.command :ask do |event|
@evnt = event
@message = event.message.content.split(" ")[1..].join(" ")
@message = event.message.content.split[1..].join(" ")
@user = find_user(discord_id: event.user.id)
event.respond error_messages[:user_not_logged_in] if user.nil?
ask_action if user
Expand Down Expand Up @@ -135,7 +135,7 @@ def disconnect_event
def speak_event
bot.command :speak do |event|
@evnt = event
@message = event.message.content.split(" ")[1..].join(" ")
@message = event.message.content.split[1..].join(" ")
@user = find_user(discord_id: event.user.id)
@chat = user.current_chat
speak_connect_checker_action
Expand Down
2 changes: 1 addition & 1 deletion lib/bots/helpers/discord_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def sl_chat_action(chat_to_select)
end

def create_chat_action
chat_title = evnt.message.content.split(" ")[1..].join(" ")
chat_title = evnt.message.content.split[1..].join(" ")
@chat = Chat.new(user_id: user.id, title: chat_title, status: 0)
chat.save ? respond_with_success : send_message(error_messages[:chat_creation])
end
Expand Down
10 changes: 4 additions & 6 deletions lib/chatgpt_assistant/audio_recognition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,22 @@ def transcribe_audio(audio_url)

def download_audio(audio_url)
audio_conn = Faraday.new(url: audio_url)
File.open(dl_file_name, "wb") do |file|
file.write(audio_conn.get.body)
end
File.binwrite(dl_file_name, audio_conn.get.body)
FFMPEG::Movie.new(dl_file_name).transcode(file_name)
File.delete(dl_file_name)
end

def header
{
"Content-Type": "multipart/form-data",
"Authorization": "Bearer #{openai_api_key}"
Authorization: "Bearer #{openai_api_key}"
}
end

def payload
{
"file": Faraday::UploadIO.new(file_name, "audio/mp3"),
"model": "whisper-1"
file: Faraday::UploadIO.new(file_name, "audio/mp3"),
model: "whisper-1"
}
end

Expand Down
8 changes: 2 additions & 6 deletions lib/chatgpt_assistant/audio_synthesis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ def synthesize_text_aws(text)
engine: "neural"
)

File.open("voice/aws-#{time}.mp3", "wb") do |file|
file.write(response.audio_stream.read)
end
File.binwrite("voice/aws-#{time}.mp3", response.audio_stream.read)
"voice/aws-#{time}.mp3"
end

Expand All @@ -86,9 +84,7 @@ def synthesize_text_ibm(text)
voice: voice
).result

File.open("voice/ibm-#{time}.mp3", "wb") do |audio_file|
audio_file.write(audio)
end
File.binwrite("voice/ibm-#{time}.mp3", audio)
"voice/ibm-#{time}.mp3"
end

Expand Down
34 changes: 17 additions & 17 deletions lib/chatgpt_assistant/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ module ChatgptAssistant
# This class is responsible for the configuration of the Chatgpt Assistant
class Config
def initialize
@env_type = ENV["ENV_TYPE"]
@language = ENV["LANGUAGE"]
@mode = ENV["MODE"]
@database_host = ENV["POSTGRES_HOST"]
@database_name = ENV["POSTGRES_DB"]
@database_username = ENV["POSTGRES_USER"]
@database_password = ENV["POSTGRES_PASSWORD"]
@openai_api_key = ENV["OPENAI_API_KEY"]
@telegram_token = ENV["TELEGRAM_TOKEN"]
@discord_token = ENV["DISCORD_TOKEN"]
@discord_client_id = ENV["DISCORD_CLIENT_ID"]
@ibm_api_key = ENV["IBM_API_KEY"]
@ibm_url = ENV["IBM_URL"]
@aws_access_key_id = ENV["AWS_ACCESS_KEY_ID"]
@aws_secret_access_key = ENV["AWS_SECRET_ACCESS_KEY"]
@aws_region = ENV["AWS_REGION"]
@discord_prefix = ENV["DISCORD_PREFIX"]
@env_type = ENV.fetch("ENV_TYPE", nil)
@language = ENV.fetch("LANGUAGE", nil)
@mode = ENV.fetch("MODE", nil)
@database_host = ENV.fetch("POSTGRES_HOST", nil)
@database_name = ENV.fetch("POSTGRES_DB", nil)
@database_username = ENV.fetch("POSTGRES_USER", nil)
@database_password = ENV.fetch("POSTGRES_PASSWORD", nil)
@openai_api_key = ENV.fetch("OPENAI_API_KEY", nil)
@telegram_token = ENV.fetch("TELEGRAM_TOKEN", nil)
@discord_token = ENV.fetch("DISCORD_TOKEN", nil)
@discord_client_id = ENV.fetch("DISCORD_CLIENT_ID", nil)
@ibm_api_key = ENV.fetch("IBM_API_KEY", nil)
@ibm_url = ENV.fetch("IBM_URL", nil)
@aws_access_key_id = ENV.fetch("AWS_ACCESS_KEY_ID", nil)
@aws_secret_access_key = ENV.fetch("AWS_SECRET_ACCESS_KEY", nil)
@aws_region = ENV.fetch("AWS_REGION", nil)
@discord_prefix = ENV.fetch("DISCORD_PREFIX", nil)
end

attr_reader :openai_api_key, :telegram_token, :discord_token, :ibm_api_key, :ibm_url,
Expand Down
2 changes: 1 addition & 1 deletion lib/chatgpt_assistant/migrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def change
t.string :discord_id, limit: 100
t.integer :platform, null: false, default: 0
t.string :name, null: false
t.integer :current_user_id, null: false, default: 0
t.integer :current_user_id, default: 0
t.timestamps
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/chatgpt_assistant/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ class Visitor < ActiveRecord::Base
has_many :visitor_actions
# has_one :tel_user, foreign_key: "telegram_id", class_name: "User"
# has_one :dis_user, foreign_key: "discord_id", class_name: "User"
validates :telegram_id, uniqueness: true
validates :discord_id, uniqueness: true
validates :name, presence: true
validates :platform, presence: true
enum platform: { telegram: 0, discord: 1 }
Expand Down

1 comment on commit 279a9cb

@codefactor-io
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CodeFactor found multiple issues last seen at 279a9cb:

Complex Code

Trailing spaces

Please sign in to comment.