Skip to content

Commit 0febb24

Browse files
authored
Merge pull request #65 from robswc/async-actions
feat: add beta support for async webhooks.
2 parents c329a5c + 814c506 commit 0febb24

File tree

7 files changed

+43
-14
lines changed

7 files changed

+43
-14
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ TVWB uses [Flask](https://flask.palletsprojects.com/en/2.2.x/) to handle the web
2727

2828
# Quickstart 📘
2929

30+
### Docker compose command
31+
32+
```bash
33+
docker-compose run app start
34+
```
35+
3036
### Installation
3137

3238
* [Docker](https://github.com/robswc/tradingview-webhooks-bot/wiki/Docker) (recommended)

docker-compose.yml

-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ services:
88
- ./src/settings.py:/app/settings.py
99
ports:
1010
- "5000:5000"
11-
network_mode: 'host'
1211
entrypoint: python3 tvwb.py

src/components/actions/async_demo.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from time import sleep
2+
3+
from components.actions.base.action import Action
4+
5+
6+
class AsyncDemo(Action):
7+
def __init__(self):
8+
super().__init__()
9+
10+
def run(self, *args, **kwargs):
11+
super().run(*args, **kwargs) # this is required
12+
"""
13+
Custom run method. Add your custom logic here.
14+
"""
15+
print(self.name, '---> action has started...')
16+
for i in range(5):
17+
print(f'{self.name} ---> {i}')
18+
sleep(1)
19+
print(self.name, '---> action has completed!')

src/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def dashboard():
5959

6060

6161
@app.route("/webhook", methods=["POST"])
62-
def webhook():
62+
async def webhook():
6363
if request.method == 'POST':
6464
data = request.get_json()
6565
if data is None:

src/requirements.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
asgiref==3.7.2
12
blinker==1.6.2
23
click==8.1.3
34
colorama==0.4.6
4-
Flask==2.3.2
5+
Flask[async]==3.0.2
56
gunicorn==20.1.0
67
importlib-metadata==6.6.0
78
itsdangerous==2.1.2
89
Jinja2==3.1.2
910
MarkupSafe==2.1.2
10-
shellingham==1.5.1
11-
typer==0.9.0
11+
shellingham==1.4.0
12+
typer==0.7.0
1213
typer-cli==0.0.13
1314
typing_extensions==4.5.0
1415
Werkzeug==2.3.3

src/settings.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# actions
2-
REGISTERED_ACTIONS = ['PrintData']
2+
REGISTERED_ACTIONS = ['PrintData', 'AsyncDemo']
33

44
# events
55
REGISTERED_EVENTS = ['WebhookReceived']
66

77
# links
8-
REGISTERED_LINKS = [('PrintData', 'WebhookReceived')]
8+
REGISTERED_LINKS = [('PrintData', 'WebhookReceived'), ('AsyncDemo', 'WebhookReceived')]
99

src/tvwb.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
from subprocess import run
34

@@ -196,16 +197,19 @@ def trigger_event(name: str):
196197
logger.info(f'Triggering event --->\t{name}')
197198
# import event
198199
event = getattr(__import__(f'components.events.{snake_case(name)}', fromlist=['']), name)()
199-
event.trigger_actions()
200+
event.trigger({})
200201
return True
201202

202203

203-
@app.command('shell')
204-
def shell():
205-
cmd = '--help'
206-
while cmd not in ['exit', 'quit', 'q']:
207-
run(f'python3 tvwb.py {cmd}'.split(' '))
208-
cmd = typer.prompt("Enter TVWB command (q) to exit")
204+
@app.command('util:send-webhook')
205+
def send_webhook(key: str):
206+
logger.info(f'Sending webhook')
207+
post_data = json.dumps({
208+
"test": "data",
209+
"key": key})
210+
# send with curl
211+
run(['curl', '-X', 'POST', '-H', 'Content-Type: application/json', '-d', post_data,
212+
'http://localhost:5000/webhook'])
209213

210214

211215
if __name__ == "__main__":

0 commit comments

Comments
 (0)