def add_job(): if request.method == 'POST': payload = request.json print(payload) if payload: try: if (len(payload['club']) is not 0): template = Template.query.filter_by( id=int(payload['template'])).first() new_data = Job() new_data.template.append(template) meta = {'type': payload['type']} new_data.meta = json.dumps(meta) for item in payload['club']: print(item) data = Club.query.filter_by(id=item['id']).first() new_data.club.append(data) db.session.add(new_data) db.session.commit() return jsonify({'success': 'Data Added'}) except Exception as e: print(str(e)) db.session.rollback() db.session.close() return jsonify({ 'message': 'Something unexpected happened. Check logs', 'log': str(e) }) else: return jsonify({'message': 'Empty Data.'}) else: return jsonify({'message': 'Invalid HTTP method . Use POST instead.'})
def add_scraper(): if request.method == 'POST': payload = request.json print(payload) if payload: try: if (len(payload['tags']) is not 0): city = City.query.filter_by( id=int(payload['city'])).first() check_data = Scrape.query.join(City, Scrape.city).filter( Scrape.city.any(City.id == city.id)).first() meta = {} meta['detail'] = str(payload['detail']) if (check_data): tags = check_data.tag check_data.meta = json.dumps(meta) for item in payload['tags']: if item not in tags: data = Tag.query.filter_by( id=item['id']).first() check_data.tag.append(data) else: new_data = Scrape(city) meta = {} meta['detail'] = str(payload['detail']) new_data.meta = json.dumps(meta) for item in payload['tags']: data = Tag.query.filter_by(id=item['id']).first() new_data.tag.append(data) db.session.add(new_data) db.session.commit() return jsonify({'success': 'Data Added'}) except Exception as e: print(str(e)) db.session.rollback() db.session.close() return jsonify({ 'message': 'Something unexpected happened. Check logs', 'log': str(e) }) else: return jsonify({'message': 'Empty Data.'}) else: return jsonify({'message': 'Invalid HTTP method . Use POST instead.'})
def test(): db.session.add(Role(names='Adminss')) db.session.add(Role(names='Moderatorss')) db.session.add(Role(names='Userss')) db.session.commit() return json.dumps({'code': '0', "msg": "source"}, ensure_ascii=False) # db.session.add_all([User(username='******',role_id=1),User(username='******',role_id=3),User(username='******',role_id=3)]) # db.session.commit()
def index(): users = Users.query.filter(Users.id > 3) lists = [] for i in users: alls = {} alls["names"] = i.name alls["ages"] = i.age lists.append(alls) print(i) print(i.name, i.age) return json.dumps({'msge': lists}, ensure_ascii=False)
def get(self, process_id): #event_service = EventService() all_commands = list() project = Project.query.filter_by(process_id=process_id).first() if project is not None: commands = project.commands.all() #event_service.analyse_command(commands, process_id) for command in commands: all_commands.append(json.dumps(command.to_dict())) return jsonify(commands=all_commands) else: return {}
def whatsapp(id): # Takes in Job ID and sends contacts job = Job.query.filter_by(id=int(id)).first() template = job.template[0] path = job.template[0].path message = job.template[0].message job.status = "running" db.session.commit() clubs = job.club meta = json.loads(job.meta) contacts = [] contact_schema = ClubContactSchema() # Needs a meta for cooloff period and which contact to start back from. for item in clubs: club = Club.query.filter_by(id = int(item.id)).first() data = contact_schema.dump(club)['total_contact'] for inner in data: contacts.append(inner) driver = driver_init() if 'last' in meta.keys(): start_range = int(meta['last']) else: start_range = int(0) end_range = start_range + 100 for contact in contacts[start_range:end_range]: try: num = contact['contact_one'] # Replace Message with Name num = num.replace('+' ,'') num = num.replace('-' ,'') num = num.replace(' ', '') name = contact['name'].split('-')[0] if message.find('{name}') is not - 1: formated_message = message.replace('{name}', name) else: formated_message = message whatsapp_send(driver , num , meta['timer'] , path , formated_message) except Exception as e: pass job.status = "done" meta['last'] = end_range job.meta = json.dumps(meta) driver.close() db.session.commit()
def run_job(): payload = request.json print(payload) job = Job.query.filter_by(id=int(payload['curr_id'])).first() meta = json.loads(job.meta) meta['timer'] = payload['timer'] job.meta = json.dumps(meta) db.session.commit() try: task = tasks.whatsapp(job.id) print("heheheheheheheeh" + task.args) job.status = "running" db.session.commit() return jsonify({'success': 'Job started'}) except Exception as e: return jsonify({'message': 'Unable to run job. Check logs'})
def start_payload_callback(payload, event): sender_id = event.sender_id page.typing_on(sender_id) page.send( sender_id, profile.last_name + ", welcome! Nice to see you here :). I'm PhillipBot. You can ask me with quick reply pop up below and you can either choose option in right menu." ) d = json.dumps({ "recipient": { "id": sender_id }, "message": { "attachment": { "type": "template", "payload": { "template_type": "button", "text": "Are you read to do this? You'll need to log in your customer's account so I can help you do more things.", "buttons": [{ "type": "account_link", "url": "https://ebotmessenger.herokuapp.com/login/authorize" }, { "title": "Term of use", "type": "web_url", "url": "www.google.com" }, { "title": "Help", "type": "postback", "payload": "HELP_PAYLOAD" }] } } } }) requests.post(url=url_messenger_message, params=params, headers=headers, data=d) page.typing_off(sender_id) print("Let's start! %s", sender_id)
def click_branch_atm_payload(payload, event): print 'location access' print "id ", event.sender_id location_request = json.dumps({ "recipient": { "id": event.sender_id }, "message": { "text": "Please send me your current location now. I will help find the nearest Branch and ATM for you.", "quick_replies": [{ "content_type": "location" }] } }) print "json: %s", location_request requests.post(url=url_messenger_message, params=params, headers=headers, data=location_request) print "data: ", data
def post_request(content): HTTPRequest().asyncPost("http://api.myjson.com/bins/", dumps(content), SlideListLoader(IO.apos), headers={'Content-Type': 'application/json'})
def put_request(content, file_name=file_name): HTTPRequest().asyncPut(file_name, dumps(content), SlideListLoader(IO.apos), headers={'Content-Type': 'application/json'})
from app import FACEBOOK_TOKEN, CASA_LINK from app import json, app, page, requests, data, QuickReply, NotificationType url_messenger_profile = 'https://graph.facebook.com/v2.6/me/messenger_profile' url_messenger_message = 'https://graph.facebook.com/v2.6/me/messages' token = FACEBOOK_TOKEN casa = CASA_LINK params = {"access_token": token} headers = {"Content-Type": "application/json"} greeting_data = json.dumps({ "greeting": [{ "locale": "default", "text": "Hello {{user_full_name}} I'm PhillipBot. Thank for getting in touch with us on Messenger. Please send us any questions you may have" }] }) requests.post(url=url_messenger_profile, params=params, headers=headers, data=greeting_data) page.show_starting_button("START_PAYLOAD") @page.callback(['START_PAYLOAD']) def start_payload_callback(payload, event): sender_id = event.sender_id page.typing_on(sender_id)