Compare commits
No commits in common. "64461ffb1ec417d4ff20ed42aef242c6968715d5" and "f236aa0aedd1fb52aa17f960240a8e970b970612" have entirely different histories.
64461ffb1e
...
f236aa0aed
@ -1,119 +0,0 @@
|
|||||||
from web3 import Web3
|
|
||||||
from bot import BankBot
|
|
||||||
|
|
||||||
SBT_ADDRESS = ''
|
|
||||||
ABI = [
|
|
||||||
{
|
|
||||||
"anonymous": False,
|
|
||||||
"inputs": [
|
|
||||||
{
|
|
||||||
"indexed": True,
|
|
||||||
"internalType": "address",
|
|
||||||
"name": "client",
|
|
||||||
"type": "address"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"indexed": True,
|
|
||||||
"internalType": "address",
|
|
||||||
"name": "bank",
|
|
||||||
"type": "address"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"indexed": False,
|
|
||||||
"internalType": "uint256",
|
|
||||||
"name": "id",
|
|
||||||
"type": "uint256"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"indexed": False,
|
|
||||||
"internalType": "uint256",
|
|
||||||
"name": "amount",
|
|
||||||
"type": "uint256"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"name": "Repay",
|
|
||||||
"type": "event"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"anonymous": False,
|
|
||||||
"inputs": [
|
|
||||||
{
|
|
||||||
"indexed": True,
|
|
||||||
"internalType": "address",
|
|
||||||
"name": "client",
|
|
||||||
"type": "address"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"indexed": True,
|
|
||||||
"internalType": "address",
|
|
||||||
"name": "shop",
|
|
||||||
"type": "address"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"indexed": True,
|
|
||||||
"internalType": "address",
|
|
||||||
"name": "bank",
|
|
||||||
"type": "address"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"indexed": False,
|
|
||||||
"internalType": "uint256",
|
|
||||||
"name": "id",
|
|
||||||
"type": "uint256"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"indexed": False,
|
|
||||||
"internalType": "uint256",
|
|
||||||
"name": "amount",
|
|
||||||
"type": "uint256"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"name": "Borrow",
|
|
||||||
"type": "event"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
class Listener():
|
|
||||||
def __init__(self, bot: BankBot) -> None:
|
|
||||||
self.w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7545'))
|
|
||||||
self.contract = self.w3.eth.contract(address=SBT_ADDRESS, abi=ABI)
|
|
||||||
self.bot = bot
|
|
||||||
|
|
||||||
def handle_repay_event(self, event):
|
|
||||||
print("Repay Event received:")
|
|
||||||
print(event['args'])
|
|
||||||
print("---")
|
|
||||||
|
|
||||||
args = event['args']
|
|
||||||
if args['client'] in self.bot.clients:
|
|
||||||
chat_id = self.bot.clients[args['client']]
|
|
||||||
self.bot.updater.bot.send_message(chat_id=chat_id, text="已經收到您編號 #{} 的帳款,共 {} ETH.".format(
|
|
||||||
args['id'], int(args['amount'])/(10**18)
|
|
||||||
))
|
|
||||||
|
|
||||||
def handle_borrow_event(self, event):
|
|
||||||
print("Borrow Event received:")
|
|
||||||
print(event['args'])
|
|
||||||
print("---")
|
|
||||||
|
|
||||||
args = event['args']
|
|
||||||
if args['client'] in self.bot.clients:
|
|
||||||
chat_id = self.bot.clients[args['client']]
|
|
||||||
self.bot.updater.bot.send_message(chat_id=chat_id, text="您已經透過本銀行向 {} 支付 {} ETH. 帳款編號(#{})".format(
|
|
||||||
args['shop'], int(args['amount'])/(10**18), args['id']
|
|
||||||
))
|
|
||||||
|
|
||||||
if args['shop'] in self.bot.clients:
|
|
||||||
chat_id = self.bot.clients[args['shop']]
|
|
||||||
self.bot.updater.bot.send_message(chat_id=chat_id, text="本銀行已經先替客戶 {} 向您支付 {} ETH. 帳款編號(#{})".format(
|
|
||||||
args['client'], int(args['amount'])/(10**18), args['id']
|
|
||||||
))
|
|
||||||
|
|
||||||
def start(self):
|
|
||||||
borrow_event_filter = self.contract.events.Borrow.create_filter(fromBlock='latest')
|
|
||||||
repay_event_filter = self.contract.events.Repay.create_filter(fromBlock='latest')
|
|
||||||
while True:
|
|
||||||
for event in borrow_event_filter.get_new_entries():
|
|
||||||
self.handle_borrow_event(event)
|
|
||||||
for event in repay_event_filter.get_new_entries():
|
|
||||||
self.handle_repay_event(event)
|
|
||||||
@ -1,12 +1,8 @@
|
|||||||
'''
|
|
||||||
python-telegram-bot: 13.7, version 20.XX cannot work
|
|
||||||
'''
|
|
||||||
|
|
||||||
from urllib import response
|
from urllib import response
|
||||||
from cairo import Filter
|
from cairo import Filter
|
||||||
from telegram import Update
|
from telegram import Update
|
||||||
import torch
|
import torch
|
||||||
from telegram.ext import MessageHandler, CallbackContext, CommandHandler, Filters, Updater
|
from telegram.ext import MessageHandler, CallbackContext, CommandHandler, filters, Application
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@ -14,12 +10,9 @@ TOKEN = ""
|
|||||||
|
|
||||||
class BankBot():
|
class BankBot():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.updater = Updater(TOKEN, use_context=True)
|
self.application = Application.builder().token(TOKEN).build()
|
||||||
self.dispatcher = self.updater.dispatcher
|
self.application.add_handler(CommandHandler('shop', self.addShop))
|
||||||
self.dispatcher.add_handler(CommandHandler('start', self.start))
|
self.application.add_handler(MessageHandler(filters.ALL, self.response))
|
||||||
self.dispatcher.add_handler(CommandHandler('add', self.add))
|
|
||||||
self.dispatcher.add_handler(MessageHandler(Filters.text, self.echo))
|
|
||||||
self.clients = {}
|
|
||||||
|
|
||||||
if os.path.isfile('./client.json'):
|
if os.path.isfile('./client.json'):
|
||||||
with open("client.json") as fp:
|
with open("client.json") as fp:
|
||||||
@ -28,23 +21,28 @@ class BankBot():
|
|||||||
self.clients = {}
|
self.clients = {}
|
||||||
|
|
||||||
def start_polling(self):
|
def start_polling(self):
|
||||||
self.updater.start_polling()
|
print("start...")
|
||||||
|
self.application.run_polling()
|
||||||
|
|
||||||
|
async def response(self, update, context):
|
||||||
|
q = update.message.text
|
||||||
|
|
||||||
|
await context.bot.send_message(
|
||||||
|
chat_id=update.effective_chat.id,
|
||||||
|
text="我看不懂這個 {} 指令".format(q)
|
||||||
|
)
|
||||||
|
|
||||||
def echo(self, update, context):
|
async def addShop(self, update, context):
|
||||||
message = update.message.text
|
|
||||||
context.bot.send_message(chat_id=update.effective_chat.id, text="我看不懂 {} 指令".format(message))
|
|
||||||
|
|
||||||
def add(self, update, context):
|
|
||||||
args = context.args
|
args = context.args
|
||||||
|
|
||||||
if len(args) != 1:
|
if len(args) != 1:
|
||||||
context.bot.send_message(chat_id=update.effective_chat.id, text="usage: /start YOUR_ADDRESS")
|
await context.bot.send_message(
|
||||||
|
chat_id=update.effective_chat.id,
|
||||||
|
text="usage: /shop [YOUR_ADDRESS]"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
address = args[0]
|
address = args[0]
|
||||||
self.clients[address] = update.effective_chat.id
|
this.client[address] = update.effective_chat.id
|
||||||
with open('client.json', 'w') as fp:
|
await context.bot.send_message(
|
||||||
json.dump(self.clients, fp)
|
chat_id=update.effective_chat.id,
|
||||||
context.bot.send_message(chat_id=update.effective_chat.id, text="開始追蹤 {} 的出入帳通知".format(address))
|
text="已經設定 {} 的店家收款通知!"
|
||||||
|
)
|
||||||
def start(self, update, context):
|
|
||||||
context.bot.send_message(chat_id=update.effective_chat.id, text="Hi, 這是暨大區塊鏈銀行 Telegram Bot\n\n/add YOUR_ADDRESS: 追蹤出入帳通知")
|
|
||||||
|
|||||||
@ -3,9 +3,6 @@ import sqlite3
|
|||||||
import os
|
import os
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
from bot import BankBot
|
from bot import BankBot
|
||||||
import time
|
|
||||||
import threading
|
|
||||||
from blockchain_listener import Listener
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
CORS(app)
|
CORS(app)
|
||||||
@ -172,17 +169,14 @@ def add_products(address):
|
|||||||
|
|
||||||
return jsonify({'status': 'OK'})
|
return jsonify({'status': 'OK'})
|
||||||
|
|
||||||
def start_flask():
|
|
||||||
app.run(host="0.0.0.0")
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
initDB()
|
initDB()
|
||||||
|
|
||||||
|
app.run(host="0.0.0.0")
|
||||||
|
|
||||||
|
print("start the bot...")
|
||||||
bot = BankBot()
|
bot = BankBot()
|
||||||
print("start polling...")
|
|
||||||
bot.start_polling()
|
bot.start_polling()
|
||||||
|
bot.application.idle()
|
||||||
flask_thread = threading.Thread(target=start_flask)
|
|
||||||
flask_thread.start()
|
|
||||||
|
|
||||||
listener = Listener(bot)
|
|
||||||
listener.start()
|
|
||||||
Loading…
Reference in New Issue
Block a user