Browse Source

Created a donations tab. Moved all kofi related stuff there, and added givebutter.

deadtom 1 tháng trước cách đây
mục cha
commit
f18b7ac1de

+ 15 - 1
ownchatbot/donation_handlers.py

@@ -94,7 +94,7 @@ def accept_kofi_sub(sub_info, sub_points):
         current_app.logger.error(f'General Exception: {aderror}')
 
 
-def save_kofi_settings(ksettings_info):  # Write rewards to kofi.py
+def save_kofi_settings(ksettings_info):  # Write settings to kofi.py
     settings_file = os.path.join(current_app.instance_path, 'kofi.py')
     try:
         with open(settings_file, 'w') as f:
@@ -108,6 +108,20 @@ def save_kofi_settings(ksettings_info):  # Write rewards to kofi.py
     return True
 
 
+def save_gb_settings(gbsettings_info):  # Write settings to givebutter.py
+    settings_file = os.path.join(current_app.instance_path, 'givebutter.py')
+    try:
+        with open(settings_file, 'w') as f:
+            f.write(f'GB_SETTINGS = {gbsettings_info}')
+        f.close
+        current_app.config.from_pyfile('givebutter.py', silent=True)  # Reread givebutter.py into the app
+    except Exception as sgbs_error:
+        current_app.logger.error(f'Couldn\'t save givebutter.py: {sgbs_error.args[0]}')
+        return False
+
+    return True
+
+
 def kofi_pngs():  # Create a list of all pngs in the kofi img dir
     png_dir = 'static/img/kofi'
     png_dir = os.path.join(current_app.root_path, png_dir)

+ 1 - 3
ownchatbot/reward_handlers.py

@@ -384,9 +384,7 @@ OCB_URL = '{config_dict['OCB_URL']}'\n\
 SECRET_KEY = '{secret_key}'  # Needed for internal Flask stuff. DO NOT DELETE.\n\
 POINTS_INTERVAL = {config_dict['POINTS_INTERVAL']}  # How long, in seconds, between points awards\n\
 POINTS_AWARD = {config_dict['POINTS_AWARD']}  # How many points awarded each interval?\n\
-PREFIX = '{config_dict['PREFIX']}'  # Preceeds commands, so OwnchatBot knows what is a command\n\
-KOFI_TOKEN = '{config_dict['KOFI_TOKEN']}'  # Needed to validate Ko-fi with OCB webhook. Get from Ko-fi Settings -> More -> API -> Webhooks -> Advanced - Verification Token.\n\
-KOFI_INTEGRATION = {config_dict['KOFI_INTEGRATION']}  # Integrate OwnchatBot with Ko-fi"
+PREFIX = '{config_dict['PREFIX']}'  # Preceeds commands, so OwnchatBot knows what is a command"
 
     try:
         with open(settings_file, 'w') as cf:

+ 88 - 49
ownchatbot/templates/mgmt.html

@@ -17,9 +17,7 @@
             <button tabindex="5" class="tablinks" data-tab="todolist" onclick="openTab(event, 'todolist')">To-Do List</button>
             <button tabindex="6" class="tablinks" data-tab="alerts" onclick="openTab(event, 'alerts')">Alerts</button>
             <button tabindex="7" class="tablinks" data-tab="settings" onclick="openTab(event, 'settings')">Settings</button>
-            {% if kofi_integration %}
-                <button class="tablinks" data-tab="kofi-settings" onclick="openTab(event, 'kofi-settings')">Kofi Settings</button>
-            {% endif %}
+            <button class="tablinks" data-tab="donations" onclick="openTab(event, 'donations')">Donations</button>
         </div>
         <img alt="Ownchat Bot logo, whimsical robot" src="/static/img/ownchatbotwide.png">
     </div>
@@ -280,16 +278,16 @@
                     </thead>
                     <tr>
                         <td> <label for="announce_enable">Enable:</label> </td>
-                        {% if settings_info[8] %}
-                        <td> <input id="announce_enable" type="checkbox" name="announce_enable" value="{{ settings_info[8] }}" checked> </td>
+                        {% if settings_info[6] %}
+                        <td> <input id="announce_enable" type="checkbox" name="announce_enable" value="{{ settings_info[6] }}" checked> </td>
                         {% else %}
-                        <td> <input id="announce_enable" type="checkbox" name="announce_enable" value="{{ settings_info[8] }}"> </td>
+                        <td> <input id="announce_enable" type="checkbox" name="announce_enable" value="{{ settings_info[6] }}"> </td>
                         {% endif %}
                         <td>Enable periodic announcements</td>
                     </tr>
                     <tr>
                         <td> <label for="announce_interval">Interval:</label> </td>
-                        <td> <input id="announce_interval" type="number" name="announce_interval" value="{{ settings_info[9] }}" size="2" required> minutes</td>
+                        <td> <input id="announce_interval" type="number" name="announce_interval" value="{{ settings_info[7] }}" size="2" required> minutes</td>
                         <td>How long between each announcement?</td>
                     </tr>
                     <tr>
@@ -335,7 +333,7 @@
                     </tr>
                     <tr>
                         <td> <label for="ocb_url">External URL:</label> </td>
-                        <td> <input id="ocb_url" type="text" name="ocb_url" size="40" value="{{ settings_info[10] }}" required> </td>
+                        <td> <input id="ocb_url" type="text" name="ocb_url" size="40" value="{{ settings_info[8] }}" required> </td>
                         <td>What is the external URL of your OwnchatBot?</td>
                     </tr>
                         
@@ -367,43 +365,19 @@
                     </tr>
                 </table>
                 
-                <h3>Kofi Integration</h3>
-                <table>
-                    <thead>
-                        <tr style="border-bottom: none;">
-                            <th style="width: 20%;"></th>
-                            <th></th>
-                            <th style="width: 50%;"></th>
-                        </tr>
-                    </thead>
-                    <tr>
-                        <td> <label for="kofi_integration">Enable:</label> </td>
-                        {% if settings_info[7] %}
-                        <td> <input id="kofi_integration" type="checkbox" name="kofi_integration" value="{{ settings_info[7] }}" checked> </td>
-                        <td>Enable Ko-fi integration.</td>
-                        {% else %}
-                        <td> <input id="kofi_integration" type="checkbox" name="kofi_integration" value="{{ settings_info[7] }}"> </td>
-                        <td>Enable Ko-fi integration. ("Kofi Settings" button will appear in the navigation bar when enabled.)</td>
-                        {% endif %}
-                    </tr>
-                    <tr>
-                        <td> <label for="kofi_token">Verification Token:</label> </td>
-                        <td style="padding: 5px;"> <input id="kofi_token" type="password" name="kofi_token" value="{{ settings_info[6] }}" size="40"> </td>
-                        <td>Get from Kofi -> More -> API -> Webhooks -> Advanced -> Verification Token.</td>
-                    </tr>
-                </table>
-                
                 <br><button id="settings form" class="button button2" type="submit">Save Changes</button><br>
             </form>
             <br><br>
         </body>
     </div>
 
-    <div id="kofi-settings" class="tabcontent">
+    <div id="donations" class="tabcontent">
         <body style="text-align: left;">
+        <h2>Donations</h2>
+        OwnchatBot can integrate with Ko-fi and GiveButter, awarding viewers points for donations and monthly subscriptions.
             <form id='kofi-settings_form' method="POST" action="/mgmt/ksettings">
                 <table>
-                <h3>Kofi Settings</h3>
+                <h3><u>Kofi</u></h3>
                 
                 <h4> Donations </h4>
                     <thead>
@@ -414,17 +388,32 @@
                         </tr>
                     </thead>
                     <tr>
-                        <td> <label for="enable_donations">Enable points for donations:</label> </td>
+                        <td> <label for="k_integration">Enable:</label> </td>
+                        {% if kofi_settings['integration'] %}
+                        <td> <input id="k_integration" type="checkbox" name="k_integration" value="{{ kofi_settings['integration'] }}" checked> </td>
+                        <td>Enable Ko-fi integration.</td>
+                        {% else %}
+                        <td> <input id="k_integration" type="checkbox" name="k_integration" value="{{ kofi_settings['integration'] }}"> </td>
+                        <td>Enable Ko-fi integration. ("Kofi Settings" button will appear in the navigation bar when enabled.)</td>
+                        {% endif %}
+                    </tr>
+                    <tr>
+                        <td> <label for="k_token">Verification Token:</label> </td>
+                        <td style="padding: 5px;"> <input id="k_token" type="password" name="k_token" value="{{ kofi_settings['token'] }}" size="40"> </td>
+                        <td>Get from Kofi -> More -> API -> Webhooks -> Advanced -> Verification Token.</td>
+                    </tr>
+                    <tr>
+                        <td> <label for="k_enable_donations">Enable points for donations:</label> </td>
                         {% if kofi_settings['donations'] %}
-                        <td> <input id="enable_donations" type="checkbox" name="enable_donations" value="{{ kofi_settings['donations'] }}" checked> </td>
+                        <td> <input id="k_enable_donations" type="checkbox" name="k_enable_donations" value="{{ kofi_settings['donations'] }}" checked> </td>
                         {% else %}
-                        <td> <input id="enable_donations" type="checkbox" name="enable_donations" value="{{ kofi_settings['donations'] }}"> </td>
+                        <td> <input id="k_enable_donations" type="checkbox" name="k_enable_donations" value="{{ kofi_settings['donations'] }}"> </td>
                         {% endif %}
                         <td>Enable awarding points for donations</td>
                     </tr>
                     <tr>
-                        <td> <label for="set_donation_points">Points per dollar:</label> </td>
-                        <td> <input id="set_donation_points" type="number" name="set_donation_points" value="{{ kofi_settings['donation_points'] }}" size="5" required> points</td>
+                        <td> <label for="k_donation_points">Points per dollar:</label> </td>
+                        <td> <input id="k_donation_points" type="number" name="k_donation_points" value="{{ kofi_settings['donation_points'] }}" size="5" required> points</td>
                         <td>How many points should viewers recieve, for every dollar they donate?</td>
                     </tr>
                     <tr>
@@ -437,24 +426,74 @@
                         <h4> Subscriptions </h4>
                             <tbody>
                                 <tr>
-                                    <td> <label for="enable_subs">Enable points for subscriptions:</label> </td>
+                                    <td> <label for="k_enable_subs">Enable points for subscriptions:</label> </td>
                                     {% if kofi_settings['subs'] %}
-                                    <td> <input id="enable_subs" type="checkbox" name="enable_subs" value="{{ kofi_settings['subs'] }}" checked> </td>
+                                    <td> <input id="k_enable_subs" type="checkbox" name="k_enable_subs" value="{{ kofi_settings['subs'] }}" checked> </td>
                                     {% else %}
-                                    <td> <input id="enable_subs" type="checkbox" name="enable_subs" value="{{ kofi_settings['subs'] }}"> </td>
+                                    <td> <input id="k_enable_subs" type="checkbox" name="k_enable_subs" value="{{ kofi_settings['subs'] }}"> </td>
                                     {% endif %}
                                     <td>Enable awarding points for monthly subscriptions</td>
                                 </tr>
                                 <tr>
-                                    <td> <label for="sub_points">Points per month:</label> </td>
-                                    <td> <input id="sub_points" type="number" name="sub_points" value="{{ kofi_settings['sub_points'] }}" size="6" required> points</td>
+                                    <td> <label for="k_sub_points">Points per month:</label> </td>
+                                    <td> <input id="k_sub_points" type="number" name="k_sub_points" value="{{ kofi_settings['sub_points'] }}" size="6" required> points</td>
                                     <td>How many points should subscribers recieve every month?</td>
                                 </tr>
                                         </tbody>
-                                    </table>
-                                </tr>
+                            </table>
+                        </tr>
+                </table>
+                <br><button id="kofi-settings_form" class="button button2" type="submit">Save</button><br>
+            </form>
+            <br><br>
+            <form id='gb-settings_form' method="POST" action="/mgmt/gbsettings">
+                <table>
+                <h3><u>GiveButter</u></h3>
+                
+                <h4> Donations </h4>
+                    <thead>
+                        <tr style="border-bottom: none;">
+                            <th style="width: 20%;"></th>
+                            <th></th>
+                            <th style="width: 50%;"></th>
+                        </tr>
+                    </thead>
+                    <tr>
+                        <td> <label for="gb_integration">Enable:</label> </td>
+                        {% if gb_settings['integration'] %}
+                        <td> <input id="gb_integration" type="checkbox" name="gb_integration" value="{{ gb_settings['integration'] }}" checked> </td>
+                        <td>Enable GiveButter integration.</td>
+                        {% else %}
+                        <td> <input id="gb_integration" type="checkbox" name="gb_integration" value="{{ gb_settings['integration'] }}"> </td>
+                        <td>Enable GiveButter integration.</td>
+                        {% endif %}
+                    </tr>
+                    <tr>
+                        <td> <label for="gb_secret">Signing Secret:</label> </td>
+                        <td style="padding: 5px;"> <input id="gb_secret" type="password" name="gb_secret" value="{{ gb_settings['secret'] }}" size="40"> </td>
+                        <td>Get this from your GiveButter dashboard. Click on the eye icon (View) next to the webhook you set up for OwnchatBot.</td>
+                    </tr>
+                    <tr>
+                        <td> <label for="gb_enable_donations">Enable points for donations:</label> </td>
+                        {% if gb_settings['donations'] %}
+                        <td> <input id="gb_enable_donations" type="checkbox" name="gb_enable_donations" value="{{ gb_settings['donations'] }}" checked> </td>
+                        {% else %}
+                        <td> <input id="gb_enable_donations" type="checkbox" name="gb_enable_donations" value="{{ gb_settings['donations'] }}"> </td>
+                        {% endif %}
+                        <td>Enable awarding points for donations</td>
+                    </tr>
+                    <tr>
+                        <td> <label for="gb_donation_points">Points per dollar:</label> </td>
+                        <td> <input id="gb_donation_points" type="number" name="gb_donation_points" value="{{ gb_settings['donation_points'] }}" size="5" required> points</td>
+                        <td>How many points should viewers recieve, for every dollar they donate?</td>
+                    </tr>
+                    <tr>
+                        <td> <label for="gb_url">GiveButter URL:</label> </td>
+                        <td style="padding: 5px;"> <input id="gb_url" type="text" name="gb_url" value="{{ gb_settings['gb_url'] }}" size="30"> </td>
+                        <td>What is your GiveButter URL?</td>
+                    </tr>
                 </table>
-                <br><button id="kofi-settings_form" class="button button2" type="submit">Save Changes</button><br>
+                <br><button id="gb-settings_form" class="button button2" type="submit">Save</button><br>
             </form>
             <br><br>
         </body>

+ 1 - 1
ownchatbot/templates/userpanel.html

@@ -47,7 +47,7 @@
                 {% set s_points_label = 'point' if sub_points == 1 else 'points' %}
                 Kofi subscribers get {{ kofi_settings['sub_points'] }} {{ s_points_label }} every month.<br>
             {% endif %}
-            <i><span style="color: #295e11;"> You must be authenticated with Owncast to earn points for Kofi donations.</span></i><br>
+            <i> You must be authenticated with Owncast to earn points for donations.</i><br>
             
             {% for user in users %}
             

+ 36 - 18
ownchatbot/web_panels.py

@@ -7,7 +7,7 @@ from ownchatbot.bot_messages import save_announce, porps
 from ownchatbot.owncast_com import send_private_chat, send_chat
 import json
 import emoji
-from ownchatbot.donation_handlers import save_kofi_settings, kofi_pngs
+from ownchatbot.donation_handlers import save_kofi_settings, kofi_pngs, save_gb_settings
 import random
 import pkce
 import requests
@@ -101,8 +101,6 @@ def mgmt():
     prefix = current_app.config['PREFIX']
     access_id = current_app.config['ACCESS_ID']
     access_token = current_app.config['ACCESS_TOKEN']
-    kofi_token = current_app.config['KOFI_TOKEN']
-    kofi_integration = current_app.config['KOFI_INTEGRATION']
     announce_enable = current_app.config['ANNOUNCE_ENABLE']
     announce_interval = current_app.config['ANNOUNCE_INTERVAL']
     announcements = current_app.config['ANNOUNCEMENTS']
@@ -117,8 +115,6 @@ def mgmt():
         prefix,
         access_token,
         owncast_url,
-        kofi_token,
-        kofi_integration,
         announce_enable,
         announce_interval,
         ocb_url
@@ -133,7 +129,7 @@ def mgmt():
                            active_rewards=active_rewards,
                            prefix=current_app.config['PREFIX'],
                            kofi_settings=current_app.config['KOFI_SETTINGS'],
-                           kofi_integration=kofi_integration,
+                           gb_settings=current_app.config['GB_SETTINGS'],
                            announcements=announcements,
                            users=users,
                            utc_timezone=utc_timezone,
@@ -341,8 +337,6 @@ def settings():
     access_token = request.form['access_token']
     owncast_url = request.form['owncast_url']
     ocb_url = request.form['ocb_url']
-    kofi_integration = 'kofi_integration' in request.form
-    kofi_token = request.form['kofi_token']
     config_dict = {
         'POINTS_INTERVAL': points_interval,
         'POINTS_AWARD': points_award,
@@ -350,9 +344,7 @@ def settings():
         'OCB_URL': ocb_url,
         'ACCESS_ID': access_id,
         'ACCESS_TOKEN': access_token,
-        'OWNCAST_URL': owncast_url,
-        'KOFI_TOKEN': kofi_token,
-        'KOFI_INTEGRATION': kofi_integration
+        'OWNCAST_URL': owncast_url
         }
     if save_config(config_dict):  # Save new config.py
         current_app.logger.info('Saved new config.')
@@ -378,24 +370,50 @@ def announcements():
     return redirect(url_for('web_panels.mgmt', activeTab='announcements'))
 
 
-@ocb.route('/mgmt/ksettings', methods=['GET', 'POST'])  # OwnchatBot settings panel
+@ocb.route('/mgmt/ksettings', methods=['GET', 'POST'])  # Ko-fi settings panel
 @requires_login
 def ksettings():
     kofi_settings_dict = current_app.config['KOFI_SETTINGS']
     if request.method == 'POST':
-        enable_donations = 'enable_donations' in request.form
-        set_donation_points = request.form['set_donation_points']
-        enable_subs = 'enable_subs' in request.form
-        sub_points = int(request.form['sub_points'])
+        kofi_integration = 'k_integration' in request.form
+        kofi_token = request.form['k_token']
+        enable_donations = 'k_enable_donations' in request.form
+        donation_points = request.form['k_donation_points']
+        enable_subs = 'k_enable_subs' in request.form
+        sub_points = int(request.form['k_sub_points'])
         kofi_url = request.form['kofi_url']
+        kofi_settings_dict['integration'] = kofi_integration
+        kofi_settings_dict['token'] = kofi_token
         kofi_settings_dict['donations'] = enable_donations
+        kofi_settings_dict['donation_points'] = donation_points
         kofi_settings_dict['subs'] = enable_subs
         kofi_settings_dict['sub_points'] = sub_points
         kofi_settings_dict['kofi_url'] = kofi_url
         if save_kofi_settings(kofi_settings_dict):
-            current_app.logger.info(f'Saved Kofi settings')
+            current_app.logger.info(f'Saved Ko-fi settings')
 
-    return redirect(url_for('web_panels.mgmt', activeTab='kofi-settings'))
+    return redirect(url_for('web_panels.mgmt', activeTab='donations'))
+
+
+@ocb.route('/mgmt/gbsettings', methods=['GET', 'POST'])  # GiveButter settings panel
+@requires_login
+def gbsettings():
+    gb_settings_dict = current_app.config['GB_SETTINGS']
+    if request.method == 'POST':
+        gb_integration = 'gb_integration' in request.form
+        gb_secret = request.form['gb_secret']
+        enable_donations = 'gb_enable_donations' in request.form
+        donation_points = int(request.form['gb_donation_points'])
+        gb_url = request.form['gb_url']
+        gb_settings_dict['integration'] = gb_integration
+        gb_settings_dict['secret'] = gb_secret
+        gb_settings_dict['donations'] = enable_donations
+        gb_settings_dict['donation_points'] = donation_points
+        gb_settings_dict['gb_url'] = gb_url
+        if save_gb_settings(gb_settings_dict):
+            current_app.logger.info(f'Saved GiveButter settings')
+
+    return redirect(url_for('web_panels.mgmt', activeTab='donations'))
 
 
 @ocb.route('/mgmt/add/<reward_type>', methods=['GET', 'POST'])