Skip to content

Commit

Permalink
add tg bot
Browse files Browse the repository at this point in the history
  • Loading branch information
ttt12334 committed Dec 25, 2023
1 parent 1730e61 commit 239362c
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/controllers/addresses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ def index
def show
@address = Address.find_by_address_hash(params[:id])
@transactions = @address.ckb_transactions.order('id desc').page(params[:page]).per(10)
@qr = RQRCode::QRCode.new("https://t.me/ckb_watch_bot?start=#{@address&.id}", :size => 7, :level => :l )
end
end
87 changes: 86 additions & 1 deletion app/views/addresses/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
<span><%= t 'address' %>
</span>
<%= render partial: 'shared/copy_to_clipboard', locals: {content: @address.address_hash,tip: t('copy_address_to_clipboard')} %>
<button data-modal-target="default-modal-tg" data-modal-toggle="default-modal-tg" class="relative inline-flex items-center text-sm font-medium text-center text-gray-500 hover:text-gray-900 focus:outline-none dark:hover:text-white dark:text-gray-400" type="button">
<svg class="w-5 h-5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 14 20">
<path d="M12.133 10.632v-1.8A5.406 5.406 0 0 0 7.979 3.57.946.946 0 0 0 8 3.464V1.1a1 1 0 0 0-2 0v2.364a.946.946 0 0 0 .021.106 5.406 5.406 0 0 0-4.154 5.262v1.8C1.867 13.018 0 13.614 0 14.807 0 15.4 0 16 .538 16h12.924C14 16 14 15.4 14 14.807c0-1.193-1.867-1.789-1.867-4.175ZM3.823 17a3.453 3.453 0 0 0 6.354 0H3.823Z"></path>
</svg>

<div class="absolute block w-3 h-3 bg-red-500 border-2 border-white rounded-full -top-0.5 start-2.5 dark:border-gray-900"></div>
</button>
</div>
<div class="break-words whitespace-normal">
<%= @address.address_hash.to_s.gsub(/(.{16}).*(.{16})/, '\1...\2') %>
Expand Down Expand Up @@ -56,6 +62,7 @@
<div class="break-words whitespace-normal">
Tx:<%= link_to transaction.tx_hash, transaction_path(transaction.tx_hash), class: 'font-medium text-blue-600 dark:text-blue-500 hover:underline' %>
<%= t 'block' %><%= link_to transaction.block_number, block_path(transaction.block_number), class: 'font-medium text-blue-600 dark:text-blue-500 hover:underline' %>
<%= t('block_timestamp') %>: <%= Time.at((transaction.block_timestamp / 1000).to_i).strftime("%Y-%m-%d %H:%M:%S") %>
</div>
</div>
<div class="flex flex-col md:flex-row">
Expand Down Expand Up @@ -110,4 +117,82 @@
</div>
</div>
<%= render 'shared/disqus' %>
</div>
</div>


<!-- tg modal -->
<div id="default-modal-tg" tabindex="-1" aria-hidden="true" style="z-index: 999" class="hidden overflow-y-auto overflow-x-hidden fixed top-0 right-0 left-0 z-50 justify-center items-center w-full md:inset-0 h-[calc(100%-1rem)] max-h-full">
<div class="relative p-4 w-full max-w-2xl max-h-full">
<!-- Modal content -->
<div class="relative bg-white rounded-lg shadow dark:bg-gray-700">
<!-- Modal header -->
<div class="flex items-center justify-between p-4 md:p-5 border-b rounded-t dark:border-gray-600">
<h3 class="text-xl font-semibold text-gray-900 dark:text-white">
<%= t('watch_address') %>
</h3>
<button type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ms-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white" data-modal-hide="default-modal-tg">
<svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"/>
</svg>
<span class="sr-only">Close modal</span>
</button>
</div>
<!-- Modal body -->
<div class="p-4 md:p-5 space-y-4">
<%= raw @qr.as_html %>


<p class="text-base leading-relaxed text-gray-500 dark:text-gray-400 break-words">
<!-- 可能是url太长了,start不能获取-->
<%= link_to t('click_here'), "https://t.me/ckb_watch_bot?start=#{@address.id}", target: '_blank', class: 'font-medium text-blue-600 dark:text-blue-500 hover:underline' %><%= t('tg_tip') %>
</p>
<p class="text-base leading-relaxed text-gray-500 dark:text-gray-400 break-words">
<%= t('tg_tip2') %>
</p>

<p class="text-base leading-relaxed text-gray-500 dark:text-gray-400 break-words">
<%= t('tg_tip3') %>
</p>

<!-- <p class="text-base leading-relaxed text-gray-500 dark:text-gray-400 break-words">-->
<%#= t('tg_tip2') %>
<!-- </p>-->

</div>
</div>
</div>
</div>
<style>
.pay_img{
height:320px;
}

.code{
width:242px;
height:242px;
/*position: relative;*/
/*margin-left: 450px;*/
/*top: 20px;*/
}
/* 二维码 */
table {
border-width: 0;
border-style: none;
border-color: #0000ff;
border-collapse: collapse;
width: 25%;
margin-left: auto;
margin-right: auto;
}

td {
border-left: solid 5px #000;
padding: 0;
margin: 0;
width: 0px;
height: 5px;
}

td.black { border-color: #000; }
td.white { border-color: #fff; }
</style>
2 changes: 1 addition & 1 deletion app/views/shared/_copy_to_clipboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<span class="copy-div">

<button data-tooltip-target="chat-bubble-example-copy-clipboard-tooltip" data-tooltip-placement="bottom" type="button" data-copy-state="copy" class="hover:text-blue-700 dark:hover:text-white copy-to-clipboard-button">
<svg class="w-3.5 h-3.5 mr-2" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 18 20">
<svg class="w-5 h-5 mr-2" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 18 20">
<path d="M5 9V4.13a2.96 2.96 0 0 0-1.293.749L.879 7.707A2.96 2.96 0 0 0 .13 9H5Zm11.066-9H9.829a2.98 2.98 0 0 0-2.122.879L7 1.584A.987.987 0 0 0 6.766 2h4.3A3.972 3.972 0 0 1 15 6v10h1.066A1.97 1.97 0 0 0 18 14V2a1.97 1.97 0 0 0-1.934-2Z"></path>
<path d="M11.066 4H7v5a2 2 0 0 1-2 2H0v7a1.969 1.969 0 0 0 1.933 2h9.133A1.97 1.97 0 0 0 13 18V6a1.97 1.97 0 0 0-1.934-2Z"></path>
</svg>
Expand Down
2 changes: 1 addition & 1 deletion app/workers/telegram_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def perform(address_id, change_balance)
bot = Telegram::Bot::Client.new(Settings.tg_bot_token)
address.tg_bots.active.each do |tg_bot|
bot.api.send_message(chat_id: tg_bot.chat_id, text: "Address: <a href='https://nervosscan.com/addresses/#{tg_bot.address_hash}'>#{tg_bot.address_hash}</a>
Change Amount: #{change_balance.to_f/10**8}", parse_mode: 'HTML')
Change Amount: #{change_balance.to_f/10**8}CKB", parse_mode: 'HTML')
end
end

Expand Down
7 changes: 6 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ en:
input: "Input"
output: "Output"
transaction_overview: "Transaction Overview"
block_timestamp: 'DateTime'
block_timestamp: 'Timestamp'
transaction_fee: 'Transaction Fee'
transaction_cycles: 'Transaction Cycles'
transaction_detail: 'Transaction Detail'
Expand All @@ -79,6 +79,11 @@ en:
copy_address_to_clipboard: 'Copied Address to clipboard!'
copy_tx_to_clipboard: 'Copied Tx to clipboard!'
support_us: 'Support Us'
watch_address: 'Watch This Address'
click_here: 'Click here'
tg_tip: ',if you have Telegram installed on this device'
tg_tip2: 'If Telegram is not installed, please install it yourself first and then click to jump.'
tg_tip3: 'Or use mobile scan the QR code.'

views:
pagination:
Expand Down
5 changes: 5 additions & 0 deletions config/locales/zh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ zh:
copy_address_to_clipboard: '复制地址到粘贴板'
copy_tx_to_clipboard: '复制Tx到粘贴板'
support_us: '支持我们'
watch_address: '监控地址'
click_here: '点击此处'
tg_tip: ',如果当前设备你已经安装了Telegram.'
tg_tip2: '如果没有安装Telegram,请先自行安装,然后点击跳转。'
tg_tip3: '或者使用手机扫描二维码。'

views:
pagination:
Expand Down
7 changes: 6 additions & 1 deletion lib/telegram_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@
bot.api.send_message(chat_id: message.chat.id, text: "You have not watching any addresses.
To start any watch, reply: /start address",parse_mode: 'HTML')
end

else
bot.api.send_message(chat_id: message.chat.id, text: "#{message.text}")
if message.text.start_with?('/start ')
hash = message.text.gsub('/start ', '').strip
if QueryKeyUtils.valid_address?(hash)
bot.api.send_message(chat_id: message.chat.id, text: "You've started watching address #{hash}")
tg_bot = TgBot.find_or_initialize_by(chat_id: message.chat.id, address_hash: hash)
tg_bot.is_use = true
tg_bot.save if tg_bot.changed?
elsif (hash.match?(/\A\d+\z/)) && address = Address.find_by(id: hash)
bot.api.send_message(chat_id: message.chat.id, text: "You've started watching address #{address.address_hash}")
tg_bot = TgBot.find_or_initialize_by(chat_id: message.chat.id, address_hash: address.address_hash)
tg_bot.is_use = true
tg_bot.save if tg_bot.changed?
else
bot.api.send_message(chat_id: message.chat.id, text: "Sorry. Address format is incorrect. Please try again.")
end
Expand Down

0 comments on commit 239362c

Please sign in to comment.