Skip to content

Instantly share code, notes, and snippets.

@csiens
Last active August 20, 2020 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csiens/69ddfe19424d0e19a961b25bcf755814 to your computer and use it in GitHub Desktop.
Save csiens/69ddfe19424d0e19a961b25bcf755814 to your computer and use it in GitHub Desktop.
wickrio parrot api
{
"builders": [{
"type": "docker",
"image": "tiangolo/uwsgi-nginx:python3.8",
"commit": "true",
"changes": ["ENTRYPOINT /start.sh"]
}],
"provisioners": [
{
"type": "file",
"source": "WickrIOParrotAPI.py",
"destination": "/tmp/main.py"
},
{
"type": "shell",
"inline": [
"pip install flask requests flask_restful",
"mv /tmp/main.py /app/main.py",
"echo 'enable-threads = true' >> /app/uwsgi.ini"
]
}
],
"post-processors": [
[
{
"type": "docker-tag",
"repository": "csiens/wickrio-parrot-api",
"tag": "0.1"
},
{
"type": "docker-push",
"login": "true",
"login_username": "csiens",
"login_password": ""
}
]
]
}
#!/usr/bin/python
# Python imports
from flask import Flask, request
from flask_restful import reqparse, Resource, Api
import requests
import os
import json
# Bot Api variables set from environment variables
bot_api_server_ip = os.getenv('APISERVERIP')
bot_api_server_port = os.getenv('APISERVERPORT')
bot_api_key = os.getenv('BOTAPIKEY')
bot_secret = os.getenv('BOTSECRET')
# Start Flask application
application = Flask(__name__)
api = Api(application)
# Send Vgroup Message
def botSendVgMessage(room, message):
bot_web_interface_URL = "http://{bot_api_server_ip}:{bot_api_server_port}/WickrIO/V1/Apps/{bot_api_key}".format(bot_api_server_ip=bot_api_server_ip, \
bot_api_server_port=bot_api_server_port, bot_api_key=bot_api_key)
bot_headers = {'Accept': '*/*', 'Content-Type': 'application/json', 'Authorization': 'Basic {bot_secret}'.format(bot_secret=bot_secret)}
data = {
"message": message,
"vgroupid": room
}
try:
sendVgMessage = requests.post(bot_web_interface_URL + "/Messages", headers=bot_headers, data=json.dumps(data))
except Exception as e:
print("Error sending message: ", e)
return "Error 500", e
print(sendVgMessage.text)
return sendVgMessage.text
# Get room list
def botGetRooms():
bot_web_interface_URL = "http://{bot_api_server_ip}:{bot_api_server_port}/WickrIO/V1/Apps/{bot_api_key}".format(bot_api_server_ip=bot_api_server_ip, \
bot_api_server_port=bot_api_server_port, bot_api_key=bot_api_key)
bot_headers = {'Accept': '*/*', 'Content-Type': 'application/json', 'Authorization': 'Basic {bot_secret}'.format(bot_secret=bot_secret)}
rooms = requests.get(bot_web_interface_URL + "/Rooms", headers=bot_headers)
print(rooms.text)
return rooms.text
parser = reqparse.RequestParser()
parser.add_argument('message')
parser.add_argument('room')
class Parrot(Resource):
def get(self):
rooms = botGetRooms()
return rooms, 200
def post(self):
args = parser.parse_args()
message = args['message']
room = args['room']
return botSendVgMessage(room, message), 201
api.add_resource(Parrot, '/parrot')
if __name__ == "__main__":
application.run(debug=True, host='0.0.0.0', port=os.getenv('LISTEN_PORT'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment