def handle_postback(raw_event): bot = Bot(utils.config["page_access_token"]) cuser = messages.user(bot.get_user_info(raw_event["sender"]["id"]), raw_event["sender"]["id"]) payload = raw_event["postback"]["payload"] red_user = utils.get_redis(cuser.id) # welcome message, user has not signed up yet if red_user == None: messages.intro_message(bot, cuser) return # if something is different between the user and the database user if red_user.is_different(cuser): utils.set_redis(cuser.id, red_user) if payload == "starting_gate": # and not red_user.looking_for_chat: if not red_user.in_chat and not red_user.looking_for_chat: red_user.set_looking_for_chat() found_chat = red_user.search_for_chat() bot.send_text_message(red_user.id, "Searching...") if found_chat: messages.found_chat_reply(bot, red_user, found_chat) elif red_user.looking_for_chat: messages.send_in_limbo(bot, red_user) elif payload == "decision_time_yes" or payload == "decision_time_no": if not red_user.on_edge: return other_user = utils.get_redis(red_user.in_chat_with) if payload == "decision_time_no": messages.decision_time_no(bot, red_user, other_user) else: if red_user.showed_id: messages.waiting_for_decision(bot, red_user) return messages.decision_time_yes(bot, red_user, other_user) elif payload == "start_message": handle_message(raw_event)
def work(cmd): s = get_redis() last_log = s.lindex(LOG_KEY, 0) if (not last_log) or last_log.split()[1] != cmd: s.lpush(LOG_KEY, '%.3f %s' % (time.time(), cmd)) s.ltrim(LOG_KEY, 0, MAX_LOG_LINES - 1) single(MQTT_CHANNEL, cmd, retain=True, **MQTT_KWARGS)
def read_log(parse_time=False): s = get_redis() ret = [] try: for line in s.lrange(LOG_KEY, 0, -1): line = line.split() ret.append({ 'time': datetime.fromtimestamp(float(line[0])), 'cmd': line[1] }) except Exception: raise return ret
def handle_auth_message(user_id, code): # get the info used for all messages bot = Bot(utils.config["page_access_token"]) cuser = utils.get_redis(user_id) new_user = False if cuser == None: cuser = messages.user(bot.get_user_info(user_id), user_id) new_user = True needs_update = cuser.needs_api_update cuser.needs_api_update = False cuser.set_api_key(code) utils.set_redis(user_id, cuser) if new_user: messages.after_registering(cuser) elif needs_update: messages.refresh_api_key()
def handle_message(raw_event): # use pymessenger to make an API call to FB to get the # info about the user who send the message bot = Bot(utils.config["page_access_token"]) user_id = raw_event["sender"]["id"] user_info = bot.get_user_info(user_id) cuser = messages.user(user_info, user_id) red_user = utils.get_redis(cuser.id) # welcome message, user has not signed up yet if red_user == None: messages.intro_message(bot, cuser) return # if something is different between the user and the database user if red_user.is_different(cuser): utils.set_redis(cuser.id, red_user) elif red_user.looking_for_chat: messages.send_in_limbo(bot, red_user) elif red_user.in_chat: if "message" in raw_event.keys(): raw_message = raw_event["message"] messages.handle_chat(bot, red_user, raw_message) else: bot.send_text_message(user_id, "You are in a chat currently") elif red_user.on_edge and not red_user.showed_id: messages.send_decision_message(bot, red_user) elif red_user.on_edge and red_user.showed_id: messages.waiting_for_decision(bot, red_user) elif not red_user.in_chat and not red_user.looking_for_chat and not red_user.on_edge: messages.send_starting_gate(bot, red_user)
def handle_chat(bot, cuser, raw_message): other_user = utils.get_redis(cuser.in_chat_with) messages_display = cuser.messages_left cuser.messages_left -= 1 other_user.messages_left -= 1 messages_left = cuser.messages_left if messages_left == 0: cuser.in_chat = False cuser.on_edge = True other_user.in_chat = False other_user.on_edge = True cuser.messages_left = 20 other_user.messages_left = 20 utils.set_redis(cuser.id, cuser) utils.set_redis(other_user.id, other_user) if "text" in raw_message.keys(): text_message = raw_message["text"] text = "{0}: '{1}'".format(messages_display, text_message) bot.send_text_message(other_user.id, text) elif "url" in raw_message["attachments"][0]["payload"].keys(): img_url = raw_message["attachments"][0]["payload"]["url"] text = "{0}:".format(messages_display) bot.send_text_message(other_user.id, text) bot.send_image_url(other_user.id, img_url) if messages_left == 0: send_decision_message(bot, cuser) send_decision_message(bot, other_user) elif messages_left == 1: bot.send_text_message(cuser.id, "You have one message left!") bot.send_text_message(other_user.id, "You have one message left!")
from settings import TOKEN from utils import get_redis db = get_redis() PUB_TOKEN = TOKEN.split(':')[0] def add_member(uid): return db.sadd('BOT:' + PUB_TOKEN, uid) def members_count(): return db.scard('BOT:' + PUB_TOKEN) def get_adv_key(): return 'BOT:%s:ADS' % PUB_TOKEN
from rq import Queue from redis import Redis from worker_jobs import * import utils import time from flask import Flask, jsonify, request import neo4j_queries app = Flask(__name__) # Tell RQ what Redis connection to use redis_conn = utils.get_redis() # define three different priority queues for jobs to be added to q_high = Queue('highdepends', connection=redis_conn) q_medium = Queue('mediumdepends', connection=redis_conn) q_low = Queue('lowdepends', connection=redis_conn) @app.route('/dependents/project', methods=['POST']) def create_parse_project_task(): if not request.json or not 'github_short_url' in request.json: return 400 task = { 'github_short_url': request.json['github_short_url'], } job = q_high.enqueue(fetch_project, task.get('github_short_url'), timeout=180)
from rq import Connection, Queue, Worker import requests import utils import os # Start a worker and begin listening on the redis job queue if __name__ == '__main__': # Tell rq what Redis connection to use with Connection(utils.get_redis()): queue = os.environ.get('RQ_QUEUE') q = Queue(queue) Worker(q).work()
def get_user_from_profile_id(profile_id): string = "-" + str(profile_id) chat_id = utils.get_redis(string) cuser = utils.get_redis(chat_id) return cuser
from __future__ import print_function from utils import get_descriptor_from_event, kp_loads, prt, get_redis import os import json import requests from worker import calc_diff, generate_descriptor REDIS_CONN = get_redis() MATCH = 'cattle_image_id_*' def register_handler(event, context): get_descriptor_from_event(event, context, REDIS_CONN, prefix='cattle_image_id_') return {'status': 'success'} def register_delete_handler(event, context): s3_info = event['Records'][0]['s3'] s3_bucket = s3_info['bucket']['name'] s3_key = s3_info['object']['key'] id_ = s3_key.split('/')[-1].split('-')[0] REDIS_CONN.delete('cattle_image_id_%s' % (id_)) def match_handler(event, context): match_image_id, match_image_descriptor, s3_client = get_descriptor_from_event(event, context, REDIS_CONN, prefix='match_image_id_')