forked from abechen/line-bot-python-heroku
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
123 lines (100 loc) · 3.63 KB
/
app.py
File metadata and controls
123 lines (100 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# encoding: utf-8
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
import requests
from decimal import Decimal
import os
app = Flask(__name__)
line_bot_api = LineBotApi(os.environ.get('LINE_ACCESS_TOKEN', '')) #Your Channel Access Token
handler = WebhookHandler(os.environ.get('LINE_SECRET', '')) #Your Channel Secret
@app.route("/callback", methods=['POST', 'GET'])
def callback():
if request.method == 'GET':
return 'Hi'
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_text_message(event):
text = event.message.text #message from user
response = ''
if text.lower() == 'knc':
response = request_coinmarketcap('kyber-network')
elif text.lower() == 'eth':
response = request_coinmarketcap('ethereum')
elif text.lower() == 'btc':
response = request_coinmarketcap('bitcoin')
elif text.lower() == 'bch':
response = request_coinmarketcap('bitcoin-cash')
elif text.lower() == 'profit':
response = calculate_eth_profit()
elif text.lower() == 'help':
response = 'Possible commands: [knc, eth, btc, bch, profit]'
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=response)) #reply the same message from user
def handle_coinmarketcap_response(r):
resp = r.json()[0]
cost = '-'
pl = '-'
sign = ''
if resp['symbol'] == 'KNC':
cost = Decimal('1.5981')
price = get_usd_price([resp])
pl, sign = get_pl(price, cost)
elif resp['symbol'] == 'ETH':
cost = Decimal('268.462')
price = get_usd_price([resp])
pl, sign = get_pl(price, cost)
return 'Symbol: {symbol}\nUSD: ${usd}'.format(
symbol=resp['symbol'],
usd=resp['price_usd']
)
def request_coinmarketcap(coin):
return handle_coinmarketcap_response(requests.get('https://api.coinmarketcap.com/v1/ticker/{}/'.format(coin)))
def calculate_profit():
cost = Decimal('1.5981')
r = requests.get('https://api.coinmarketcap.com/v1/ticker/kyber-network/')
price = get_usd_price(r.json())
pl, sign = get_pl(price, cost)
pl_in_thb = pl/100 * 120000
return 'Cost: ${}\nPrice: ${}\nP/L: {}{}%\nP/L(THB): {}{}'.format(
cost, price, sign, pl.quantize(Decimal('1.00')),
sign, pl_in_thb.quantize(Decimal('1.00')))
def calculate_eth_profit():
cost = Decimal('120000')
amount = Decimal('11.079')
r = requests.get('https://bx.in.th/api/')
eth = r.json()['21']
last_price = Decimal(eth['last_price'])
value = amount * last_price
pl = value-cost
pl_percent = pl*100/cost
sign = '+' if pl > 0 else ''
return 'Cost:\t\t {} THB\nCurrent:\t\t {} THB\nP/L:\t\t {}{} THB({}{}%)'.format(
cost, value, sign, pl.quantize(Decimal('1.00')),
sign, pl_percent.quantize(Decimal('1.00')))
def get_usd_price(r):
return Decimal(r[0]['price_usd']).quantize(Decimal('1.0000'))
def get_pl(price, cost):
pl = ((price-cost)*100/cost).quantize(Decimal('1.0000'))
sign = '+' if pl > 0 else ''
return pl, sign
if __name__ == "__main__":
app.run(host='0.0.0.0',port=int(os.environ.get('PORT', 80)))