From 0b72d1e4811b36af8a1afd897ea4be16c162b576 Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Sat, 2 Dec 2023 16:32:39 +0000 Subject: [PATCH] Add TelegramWallet to bots --- app/models/bot.rb | 34 +++++++++++++------ app/models/telegram_wallet.rb | 4 +-- .../20231201182512_create_telegram_wallet.rb | 2 +- db/schema.rb | 2 +- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/models/bot.rb b/app/models/bot.rb index e26c3f60..a6b81a70 100644 --- a/app/models/bot.rb +++ b/app/models/bot.rb @@ -32,7 +32,7 @@ class Bot < ActiveRecord::Base # eval(IO.read('./app/models/commands/fortune.rb'), binding) case message.text when /^\/fortune/i - response_text = `fortune -a` + response_text = `fortune` begin bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: response_text, parse_mode: "Markdown") rescue @@ -72,23 +72,37 @@ class Bot < ActiveRecord::Base end end case message.text - when /^\/fortune/i - response_text = `fortune` - begin - bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: response_text, parse_mode: "Markdown") - rescue - puts "Error from fortune" - end - end - case message.text when /^\/wallet/i, '/mywallet' # mywallet(message.from.id, bot, message) # unless get_wallet(message.from.id) # create_wallet(message.from.id, bot, message) # end + telegram_wallet = get_or_create_telegram_wallet(message.from.id) +response_text = "Hello #{message.from.first_name} +ICP Account Id: +``` +#{telegram_wallet.get_icp_id} +``` +ICP Principal: +``` +#{telegram_wallet.get_icp_principal} +``` +" + begin + bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: response_text, parse_mode: "Markdown") + rescue + puts "Error from mywallet" + end end end end end + + def get_or_create_telegram_wallet(telegram_id) + telegram_wallet = TelegramWallet.where(telegram_id: telegram_id).first_or_initialize + telegram_wallet.save + return telegram_wallet + end + end diff --git a/app/models/telegram_wallet.rb b/app/models/telegram_wallet.rb index 111d98e0..9b81ca75 100644 --- a/app/models/telegram_wallet.rb +++ b/app/models/telegram_wallet.rb @@ -1,5 +1,5 @@ class TelegramWallet < ActiveRecord::Base - def icprincipal + def get_icp_principal return self.icp_principal if self.icp_principal if self.icp_id.nil? if !self.telegram_id.nil? @@ -27,7 +27,7 @@ class TelegramWallet < ActiveRecord::Base end return self.icp_principal end - def icid + def get_icp_id return self.icp_id if self.icp_id if self.icp_principal.nil? if !self.telegram_id.nil? diff --git a/db/migrate/20231201182512_create_telegram_wallet.rb b/db/migrate/20231201182512_create_telegram_wallet.rb index 32b76a66..97cc2e81 100644 --- a/db/migrate/20231201182512_create_telegram_wallet.rb +++ b/db/migrate/20231201182512_create_telegram_wallet.rb @@ -1,7 +1,7 @@ class CreateTelegramWallet < ActiveRecord::Migration[6.1] def change create_table :telegram_wallets do |t| - t.integer :telegram_id, null: false + t.string :telegram_id, null: false t.string :icp_id t.string :icp_principal diff --git a/db/schema.rb b/db/schema.rb index 74f12172..1c456786 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -203,7 +203,7 @@ ActiveRecord::Schema.define(version: 2023_12_01_182512) do end create_table "telegram_wallets", force: :cascade do |t| - t.integer "telegram_id", null: false + t.string "telegram_id", null: false t.string "icp_id" t.string "icp_principal" t.datetime "created_at", precision: 6, null: false