Add basic Telegram ICP wallet

This commit is contained in:
Jesse C. Fisher 2023-12-01 18:59:50 +00:00
parent 3f836a5885
commit 4eec2bd563
4 changed files with 95 additions and 1 deletions

View File

@ -71,6 +71,23 @@ class Bot < ActiveRecord::Base
puts "Error from start"
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
end
end
end
end

View File

@ -0,0 +1,58 @@
class TelegramWallet < ActiveRecord::Base
def icprincipal
return self.icp_principal if self.icp_principal
if self.icp_id.nil?
if !self.telegram_id.nil?
if `dfx identity use #{self.telegram_id}; echo $?`.to_i == 255
`dfx identity new #{self.telegram_id} --storage-mode=plaintext`
`dfx identity use #{self.telegram_id}`
self.icp_principal = `dfx identity get-principal`.strip
self.icp_id = `dfx ledger account-id`.strip
`dfx identity use anonymous`
self.save
else
# Identity already exist
self.icp_id = `dfx ledger account-id`.strip
self.icp_principal = `dfx identity get-principal`.strip
`dfx identity use anonymous`
self.save
end
end
elsif
`dfx identity use #{self.telegram_id}`
self.icp_id = `dfx ledger account-id`.strip
self.icp_principal = `dfx identity get-principal`.strip
`dfx identity use anonymous`
self.save
end
return self.icp_principal
end
def icid
return self.icp_id if self.icp_id
if self.icp_principal.nil?
if !self.telegram_id.nil?
if `dfx identity use #{self.telegram_id}; echo $?`.to_i == 255
`dfx identity new #{self.telegram_id} --storage-mode=plaintext`
`dfx identity use #{self.telegram_id}`
self.icp_principal = `dfx identity get-principal`.strip
self.icp_id = `dfx ledger account-id`.strip
`dfx identity use anonymous`
self.save
else
# Identity already exist
self.icp_id = `dfx ledger account-id`.strip
self.icp_principal = `dfx identity get-principal`.strip
`dfx identity use anonymous`
self.save
end
end
elsif
`dfx identity use #{self.telegram_id}`
self.icp_id = `dfx ledger account-id`.strip
self.icp_principal = `dfx identity get-principal`.strip
`dfx identity use anonymous`
self.save
end
return self.icp_id
end
end

View File

@ -0,0 +1,11 @@
class CreateTelegramWallet < ActiveRecord::Migration[6.1]
def change
create_table :telegram_wallets do |t|
t.integer :telegram_id, null: false
t.string :icp_id
t.string :icp_principal
t.timestamps
end
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2023_11_29_171945) do
ActiveRecord::Schema.define(version: 2023_12_01_182512) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -202,6 +202,14 @@ ActiveRecord::Schema.define(version: 2023_11_29_171945) do
t.index ["slug"], name: "index_tables_on_slug", unique: true
end
create_table "telegram_wallets", force: :cascade do |t|
t.integer "telegram_id", null: false
t.string "icp_id"
t.string "icp_principal"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "tokens", force: :cascade do |t|
t.string "canister_id"
t.integer "standard"