Beispiel #1
0
def start_game(message):
    ph_number = message.who.split("@")[0]
    params = message.predicate.split(" ")
    if len(params) != 1:
        mac.send_message(
            'Sorry there is something wrong in your stake command!')
        return
    (group_name) = params
    mac.send_message(
        u"📡 Ok, let's play ! But first, wait me to contact the server guy",
        message.conversation)
    payload = {'wa_group_name': group_name, 'wa_ph_number': ph_number}
    start_game_post = requests.post(
        "http://{}/api/whatsappbot/start".format(BASE_IP), data=payload)
    start_game_data = start_game_post.json()
    if not start_game_data or not start_game_data['message']:
        mac.send_message(
            "I failed to start the game session, no message field",
            message.conversation)
        return
    if not helper.isResponseSuccess(start_game_post.status_code):
        mac.send_message(
            'I failed to start the game, here some error \n {}'.format(
                start_game_data['message']), message.conversation)
        return
    mac.send_message(GAME_STARTED, message.who)
Beispiel #2
0
def end_game(message, group_name):
    group_id = message.conversation.split("@")[0]
    ph_number = message.who.split("@")[0]
    payload = {'wa_group_name': group_name, 'wa_ph_number': ph_number}
    end_game_post = requests.post(
        "http://{}/api/whatsappbot/end".format(BASE_IP), data=payload)
    end_game_data = end_game_post.json()
    if not end_game_data or not end_game_data['message']:
        mac.send_message("I failed to end the game session, no message field",
                         message.conversation)
        return
    if not helper.isResponseSuccess(end_game_post.status_code):
        mac.send_message(
            'I failed to end the game, here what the server guy say: \n {}'.
            format(end_game_data['message']), message.conversation)
        return
    if not end_game_data['wa_group_id']:
        mac.send_message("You are missing group", message.conversation)
        return
    wa_group_id = end_game_data['wa_group_id'] + "@g.us"
    winner_lists = end_game_data["winner_list"]
    if len(winner_lists) == 0:
        mac.send_message("There isn't any winner in this game", wa_group_id)
    winner_rows = [["phone_number", "stakes", "profit"]]
    for winner in winner_lists:
        winner_rows.append(
            [winner["phone_number"], winner["stake"], winner["profit"]])
    table = texttable.Texttable()
    table.add_rows(winner_rows)
    mac.send_message(
        u'🏁 We have reach the end of the game 🏁 \n '
        u'🥁🥁🥁 Now I\'ll announce the winners! 🥁🥁🥁', wa_group_id)
    mac.send_message(table.draw(), wa_group_id)
    send_image(wa_group_id, end_game_data["winner_stake"])
Beispiel #3
0
def create_group(message):
    params = message.predicate
    print(params)
    if len(params) == 0:
        ambigous("You need to initiate group name", message)
        return
    if len(params) > 24:
        ambigous(
            "Group Name maximal character is 24. Yours is {}".format(
                len(params)), message)
        return
    group_name = params
    ph_number = message.who.split("@")[0]
    group_id = '{}_{}'.format(ph_number, group_name)
    payload = {"wa_group_name": group_name, "wa_ph_number": ph_number}
    group_check_post = requests.post(
        'http://{}/api/whatsappbot/create_group'.format(BASE_IP), data=payload)
    group_check_data = group_check_post.json()
    is_successful = helper.isResponseSuccess(group_check_post.status_code)
    if not is_successful:
        mac.send_message(
            'Sorry you are failed to create group. the error is: \n',
            message.conversation)
        mac.send_message(group_check_data['message'], message.conversation)
        return

    mac.create_group(ph_number,
                     group_name,
                     message.conversation,
                     callback=update_group)
Beispiel #4
0
    def get_info(result):
        group_name = result.subject
        payload = {'wa_group_id': group_id, 'wa_ph_number': ph_number}
        register_post = requests.post(
            'http://{}/api/whatsappbot/register_group'.format(BASE_IP),
            data=payload)
        register_data = register_post.json()
        is_successful = helper.isResponseSuccess(register_post.status_code)
        if not register_data:
            mac.send_message('Sorry I Failed connecting to server',
                             message.conversation)
            return
        if not is_successful:
            #mac.send_message('Sorry you are already registered in {} Group \n '.format(group_name), message.who)
            mac.send_message(register_data['message'], message.who)
            print(register_post.text)
            return

        mac.send_message(
            "{} Success join the game! Good luck!".format(user_phone_star),
            message.conversation)
        mac.send_message(register_data['message'], message.who)
        if not register_data['init_point']:
            return
        mac.send_message(
            "Congratz! You successfully join {} Group. Your initial balance is {}  "
            .format(group_name, register_data['init_point']), message.who)
Beispiel #5
0
def register_user(message):
    user_number = message.who.split("@")[0]
    group_id = message.conversation.split("@")[0]
    if (not user_number or not group_id):
        message.send_message("Error when registering user",
                             message.conversation)
        return
    payload = {'wa_group_id': group_id, 'wa_ph_number': user_number}
    register_post = requests.post(
        'http://{}/trivia/api/whatsappbot/register_group'.format(BASE_IP),
        data=payload)
    register_data = register_post.json()
    is_successful = helper.isResponseSuccess(register_post.status_code)
    if not register_data:
        mac.send_message('Sorry I Failed connecting to server',
                         message.conversation)
        return
    if not is_successful:
        mac.send_message(
            'Sorry Failed to register You to server, your error is \n ',
            message.conversation)
        mac.send_message(register_data['message'], message.conversation)
        print(register_post.text)
        return
    mac.send_message("Great !", message.conversation)
    mac.send_message(register_data['message'], message.conversation)
    print(register_post.text)
Beispiel #6
0
def set_init_balance(message):
    ph_number = message.who.split("@")[0]
    params = message.predicate.split(" ")
    init_point = params[0]
    group_name = " ".join(params[1:])
    # is_in_group = helper.conversationIsGroup(message)
    # if not is_in_group:
    #     mac.send_message("Hello, I can't set your from here, please do it from your group.", message.conversation)
    #     return
    if len(params) < 2:
        mac.send_message(
            "Please specify the default balance and the rtp ammount for your players",
            message.conversation)
        mac.send_message("You can ask for my #help if you are not sure",
                         message.conversation)
        return
    if len(params) > 10:
        mac.send_message(
            "Please check your command again, it seems it contain too much arguments",
            message.conversation)
        mac.send_message(
            "You can ask for my #help if you are not sure how to setup",
            message.conversation)
        return
    is_number_only = helper.isAllNumber(init_point)
    if not is_number_only:
        mac.send_message(
            "Hey, I can't really understand your command. Please only use number for init_point",
            message.conversation)
        return

    mac.send_message("Setting the group..", message.conversation)
    group_id = message.conversation.split("@")[0]
    payload = {
        'wa_ph_number': ph_number,
        'init_point': init_point,
        'wa_group_name': group_name
    }
    set_group_post = requests.post(
        'http://{}/api/whatsappbot/init_balance'.format(BASE_IP), data=payload)
    set_group_data = set_group_post.json()
    if not set_group_data or not set_group_data['message']:
        mac.send_message("I Failed to setup the group, no message field",
                         message.conversation)
        return
    if not helper.isResponseSuccess(set_group_post.status_code):
        mac.send_message(
            'I failed to setup the group, here is the message \n {}'.format(
                set_group_data['message']), message.conversation)
        return
    #mac.send_message(GROUP_FINISH_SETTING, message.conversation)
    mac.send_message(
        'We already set the initial balance of member is {} credit.'.format(
            init_point), message.who)
    mac.send_message(STAKE_MESSAGE, message.who)
Beispiel #7
0
def create_stake(message):
    params = message.predicate.split(" ")

    if message.conversation == message.who:
        mac.send_message(
            "You can't place a stake from here, please place a stake from the group",
            message.who)
        return
    if len(params) < 2:
        mac.send_message(
            "Too few arguments ! you should specify animal type and the value",
            message.conversation)
        return
    if len(params) > 2:
        mac.send_message(
            "I can't understand your stake, seems it contain too much arguments !",
            message.conversation)
        return
    user_phone = message.who.split("@")[0]
    group_id = message.conversation.split("@")[0]
    stake = params[0]
    value = params[1]

    if not helper.isAllNumber(value):
        mac.send_message(
            'Sorry I cannot understand the currency yet please just use plain number for value',
            message.conversation)
        return

    payload = {
        'wa_group_id': group_id,
        'wa_ph_number': user_phone,
        'stake': stake,
        'value': value
    }
    stake_post = requests.post(
        'http://{}/trivia/api/whatsappbot/stakes'.format(BASE_IP),
        data=payload)
    stake_post_data = stake_post.json()
    is_successful = helper.isResponseSuccess(stake_post.status_code)
    if not stake_post_data or not stake_post_data['message']:
        mac.send_message('ERROR connecting to server', message.conversation)
        return
    if not is_successful:
        mac.send_message(stake_post_data['message'], message.who)
        return
    mac.send_message("💰 {} place a stake!".format(message.who_name),
                     message.conversation)
    mac.send_message(
        "{} your stake is {}".format(stake_post_data['message'],
                                     message.predicate), message.who)
Beispiel #8
0
def set_group(message):
    params = message.predicate.split(" ")
    is_in_group = helper.conversationIsGroup(message)
    if not is_in_group:
        mac.send_message(
            "Hello, I can't set your from here, please do it from your group.",
            message.conversation)
        return
    if len(params) < 2:
        mac.send_message(
            "Please specify the default balance and the rtp ammount for your players",
            message.conversation)
        mac.send_message("You can ask for my #help if you are not sure",
                         message.conversation)
        return
    if len(params) > 2:
        mac.send_message(
            "Please check your command again, it seems it contain too much arguments",
            message.conversation)
        mac.send_message(
            "You can ask for my #help if you are not sure how to setup",
            message.conversation)
        return
    for param in params:
        is_number_only = helper.isAllNumber(param)
        if not is_number_only:
            mac.send_message(
                "Hey, I can't really understand your command. Please only use number for balance and rtp",
                message.conversation)
            return

    mac.send_message("Setting the group..", message.conversation)
    group_id = message.conversation.split("@")[0]
    (balance, rtp) = params
    payload = {'wa_group_id': group_id, 'init_point': balance, 'rtp': rtp}
    set_group_post = requests.post(
        'http://{}/trivia/api/whatsappbot/update_point'.format(BASE_IP),
        data=payload)
    set_group_data = set_group_post.json()
    if not set_group_data or not set_group_data['message']:
        mac.send_message("I Failed to setup the group, no message field",
                         message.conversation)
        return
    if not helper.isResponseSuccess(set_group_post.status_code):
        mac.send_message(
            'I failed to setup the group, here is the message \n {}'.format(
                set_group_data['message']), message.conversation)
        return
    mac.send_message(GROUP_FINISH_SETTING, message.conversation)
Beispiel #9
0
def set_game(message):
    ph_number = message.who.split("@")[0]
    params = message.predicate.split(" ")
    rtp = params[0]
    hours = params[1]
    group_name = " ".join(params[2:])

    if len(params) > 10:
        mac.send_message("I Failed to setup the game", message.who)

    if not helper.isAllNumber(rtp):
        mac.send_message(
            'Sorry there is something wrong in your stake command!',
            message.who)
        return

    if int(rtp) > 101:
        mac.send_message(
            'Sorry there is something wrong in your stake command! \n Please set rtp between 1 and 100',
            message.who)
        return

    group_id = message.conversation.split("@")[0]
    payload = {
        'rtp': rtp,
        'hours': hours,
        'wa_ph_number': ph_number,
        'wa_group_name': group_name
    }
    start_game_post = requests.post(
        "http://{}/api/whatsappbot/game".format(BASE_IP), data=payload)
    start_game_data = start_game_post.json()
    if not start_game_data or not start_game_data['message']:
        mac.send_message(
            "I failed to start the game session, no message field",
            message.conversation)
        return
    if not helper.isResponseSuccess(start_game_post.status_code):
        mac.send_message(
            'I failed to start the game, here some error \n {}'.format(
                start_game_data['message']), message.conversation)
        return
    #print(start_game_data['message'])
    mac.send_message(start_game_data['message'], message.who)
Beispiel #10
0
def start_game(message):
    mac.send_message(
        u"📡 Ok, let's play ! But first, wait me to contact the server guy",
        message.conversation)
    group_id = message.conversation.split("@")[0]
    payload = {'wa_group_id': group_id}
    start_game_post = requests.post(
        "http://{}/trivia/api/whatsappbot/start_game".format(BASE_IP),
        data=payload)
    start_game_data = start_game_post.json()
    if not start_game_data or not start_game_data['message']:
        mac.send_message(
            "I failed to start the game session, no message field",
            message.conversation)
        return
    if not helper.isResponseSuccess(start_game_post.status_code):
        mac.send_message(
            'I failed to start the game, here some error \n {}'.format(
                start_game_data['message']), message.conversation)
        return
    mac.send_message(GAME_STARTED, message.conversation)
Beispiel #11
0
def create_stake(message):
    params = message.predicate.split(" ")

    def is_admin(result):
        if result:
            mac.send_message(
                "Sorry, you can not place a stake in your own group",
                message.who)
        return

    check_group(message, callback=is_admin)

    if message.conversation == message.who:
        mac.send_message(
            "You can't place a stake from here, please place a stake from the group",
            message.who)
        return

    if len(params) == 1:
        mac.send_message(HELP_MESSAGE, message.who)
        return

    if len(params) < 2:
        mac.send_message(
            "Too few arguments ! you should specify animal type and the value",
            message.who)
        return

    if len(params) > 2:
        mac.send_message(
            "I can't understand your stake, seems it contain too much arguments !",
            message.who)
        return

    (stake, value) = params

    user_phone = message.who.split("@")[0]
    user_phone_star = user_phone[:5] + "****"
    group_id = message.conversation.split("@")[0]

    if not helper.isAllNumber(value):
        mac.send_message(
            'Sorry there is something wrong in your stake command! \n Please use #stake [animal name] [amount]',
            message.who)
        return

    if int(value) < 1:
        mac.send_message(
            'Sorry there is something wrong in your stake command! \n Please use #stake [animal name] [amount]',
            message.who)
        return

    if not stake in ALLANIMALS:
        mac.send_message(
            'Sorry there is something wrong in your stake command! \n Please use #stake [animal name] [amount]',
            message.who)
        return

    payload = {
        'wa_group_id': group_id,
        'wa_ph_number': user_phone,
        'stake': stake,
        'value': value
    }
    stake_post = requests.post(
        'http://{}/api/whatsappbot/stakes'.format(BASE_IP), data=payload)
    stake_post_data = stake_post.json()
    is_successful = helper.isResponseSuccess(stake_post.status_code)
    if not stake_post_data or not stake_post_data['message']:
        mac.send_message('ERROR connecting to server', message.conversation)
        return
    if not is_successful:
        mac.send_message(stake_post_data['message'], message.who)
        return
    mac.send_message("💰 {} place a stake!".format(user_phone_star),
                     message.conversation)
    mac.send_message("{}".format(stake_post_data['message']), message.who)