【发布时间】:2022-01-25 07:58:26
【问题描述】:
我正在用 python 构建一个 coinbase 机器人,它使用 twilio 来获取我的 coinbase 投资组合,并使用当前余额向我的手机发送一条短信。我正在尝试但现在正在努力做的事情是根据我发送回 twilio 号码的消息进行 coinbase 交易。
为了让 twilio 接收我的消息,我必须设置一个小型烧瓶服务器并将环境集中到一个公共 ngrok url,然后我将其作为 webhook url 附加到我的 twilio 帐户设置中以接收消息。
这是我的烧瓶:
msg_app.py
from twilio.rest import Client as twilioClient
from flask import Flask, request, redirect
from twilio.twiml.messaging_response import MessagingResponse
from pyngrok import ngrok
app = Flask(__name__)
@app.route("/sms", methods=['GET', 'POST'])
def sms_reply():
"""Fetch message response to extract order data"""
body = request.values.get('Body', None)
resp = MessagingResponse()
resp.message('test')
return str(resp)
if __name__ == "__main__":
app.run(debug=True)
差不多,我将cd 放入保存此脚本的目录(msg_app.py),然后运行msg_app.py 运行以启动服务器。然后,我将在不同的 cmd 提示符下运行 ngrok http 5000(5000 是本地主机端口)来启动漏斗。
如果我在手机上向 Twilio 号码发送消息,例如 'Hello',我会收到 test 作为回复,但是如何在我的机器人脚本中收到 'Hello' 消息,那么我可以根据该消息的内容调用逻辑吗?例如,这是我的机器人:
coinbase_bot.py
import dotenv
import selenium
import coinbase
import requests
from coinbase.wallet.client import Client
from dotenv import load_dotenv
import os
import sys
import re
from twilio.rest import Client as twilioClient
from flask import Flask, request, redirect
from twilio.twiml.messaging_response import MessagingResponse
import msg_app
from msg_app import sms_reply
sys.path.insert(0, os.environ['USERPROFILE'])
dotenv_path = os.environ['USERPROFILE'] + '\prod.env'
load_dotenv(dotenv_path, override=True)
api_key = os.getenv('coinbase_api_key')
api_secret = os.getenv('coinbase_api_secret')
# TWILIO
account_sid = os.getenv('twilio_account_sid')
auth_token = os.getenv('twilio_auth_token')
twilio_number = os.getenv('twilio_number')
# Clients
coinbase_client = Client(api_key, api_secret)
twilio_client = twilioClient(account_sid, auth_token)
# user = coinbase_client.get_current_user()
accounts = coinbase_client.get_accounts()
currencies = coinbase_client.get_currencies()
id = accounts['data'][0]['id']
class Bot:
def __init__(self, accounts):
self.accounts = accounts
self.account_id = self.accounts['data'][0]['id']
# self.method_id =
def get_current_price(self, ticker):
for coin in accounts['data']:
if coin['currency'] == ticker:
value = "$" + str(coin['native_balance']['amount'])
self.value = value
return self.value
def send_notification(self):
price = self.get_current_price('LRC')
self.message = twilio_client.messages.create(
body='The price of your portfolio is currently: ' + str(price),
from_=twilio_number,
to='+1555555555')
def fetch_payment_method(self):
payment_method = coinbase_client.get_buys(self.account_id)
self.method_id = payment_method[0]['payment_method']['id']
# set up twilio, to end notification when price is below certain amount
def execute_sales(self, account_id, amount, currency, method_id):
coinbase_client.sell(account_id=account_id, amount=amount,
currency=currency, method_id=method_id)
def receive(self, msg):
self.ticker = "".join(re.findall(r'[A-Z]{3}', msg))
self.amount_to_sell = "".join(re.findall(r'[0-9]', msg))
b.execute_sales(account_id=self.account_id, currency=self.ticker,
amount=amount_to_sell, method_id=self.method_id)
# based on SMS response, method executes trade
b = Bot(accounts)
我想运行接收函数以根据消息内容执行销售。我可以这样做吗?我试图从烧瓶脚本中提取正文内容,例如:body = request.values.get('Body', None),并以某种方式将此结果发送回机器人。但是,由于 msg_app 就像服务器脚本,我怎样才能在我的机器人中调用该脚本,而不会弄乱服务器?
也许我应该将机器人包装在一个 while 循环中,并将接收放在一个 try/except 子句中?
或者,也许我对这一切的运作方式了解有限。希望我是清楚的。
【问题讨论】: