Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 17 additions & 199 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@


from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackQueryHandler
from telegram.ext import ConversationHandler, JobQueue
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, error
from instagrapi import exceptions, Client

import os
import sys
import configparser
Expand All @@ -15,6 +12,9 @@
import threading
import random
import time
import os

DATABASE_URL = os.getenv('DATABASE_URL', 'sqlite:///bot_data.db')


def load_settings() -> None:
Expand All @@ -28,12 +28,12 @@ def load_settings() -> None:


def initiate_db() -> None:
conn = sqlite3.connect('bot_data.db') # creates the file if doesn't exists, or connect if exists
conn = sqlite3.connect(DATABASE_URL) # creates the file if doesn't exists, or connect if exists
c = conn.cursor() # set up a cursor to execute SQL commands
with conn:
c.execute("CREATE TABLE IF NOT EXISTS users (user_id INTEGER PRIMARY KEY, username TEXT,"
c.execute("CREATE TABLE IF NOT EXISTS users (user_id INTEGER PRIMARY KEY, username TEXT,",
"profile_link TEXT, balance REAL, pending REAL, is_admin BOOL)")
c.execute("CREATE TABLE IF NOT EXISTS follows (action_id INTEGER PRIMARY KEY, follower_id INTEGER,"
c.execute("CREATE TABLE IF NOT EXISTS follows (action_id INTEGER PRIMARY KEY, follower_id INTEGER,",
"follower_profile TEXT, followee_id INTEGER, followee_profile TEXT, status TEXT, follow_date TEXT)")


Expand All @@ -44,7 +44,7 @@ def is_admin(user_id) -> bool:


def add_action(follower_id: int, follower_profile: str, followee_profile: str, followee_id: int, status: str) -> None:
conn = sqlite3.connect("bot_data.db")
conn = sqlite3.connect(DATABASE_URL)
c = conn.cursor()
with conn:
c.execute("INSERT INTO follows (follower_id, follower_profile, followee_id, followee_profile, "
Expand All @@ -56,7 +56,7 @@ def add_action(follower_id: int, follower_profile: str, followee_profile: str, f


def user_exists(user_id: int) -> bool:
conn = sqlite3.connect('bot_data.db') # creates the file if doesn't exists, or connect if exists
conn = sqlite3.connect(DATABASE_URL) # creates the file if doesn't exists, or connect if exists
c = conn.cursor() # set up a cursor to execute SQL commands
with conn:
c.execute("SELECT * from users WHERE (user_id = :user_id)",
Expand All @@ -67,7 +67,7 @@ def user_exists(user_id: int) -> bool:


def has_profile_link(user_id: int) -> bool:
conn = sqlite3.connect('bot_data.db') # creates the file if doesn't exists, or connect if exists
conn = sqlite3.connect(DATABASE_URL) # creates the file if doesn't exists, or connect if exists
c = conn.cursor() # set up a cursor to execute SQL commands
with conn:
c.execute("SELECT * from users WHERE (user_id = :user_id AND profile_link IS NOT NULL)",
Expand Down Expand Up @@ -101,7 +101,7 @@ def is_follower(instagram_bot, follower_profile: str, followee_profile: str) ->


def check_action(follower_id: int, followee_id: int) -> int:
conn = sqlite3.connect("bot_data.db")
conn = sqlite3.connect(DATABASE_URL)
c = conn.cursor()
with conn:
c.execute("SELECT follow_date from follows WHERE (follower_id = :follower_id and followee_id = :followee_id)"
Expand All @@ -123,7 +123,7 @@ def update_session(user_id: int, update, context) -> None:


def balance_to_pending(user_id: int, amount: float) -> None:
conn = sqlite3.connect("bot_data.db")
conn = sqlite3.connect(DATABASE_URL)
c = conn.cursor()
with conn:
c.execute("SELECT balance, pending FROM users WHERE user_id = :user_id", {'user_id': user_id})
Expand All @@ -137,7 +137,7 @@ def balance_to_pending(user_id: int, amount: float) -> None:

def transfer_points(sending_user_id: int, receiving_user_id: int, amount: float) -> bool:
print("transfering from {} to {}. {}$".format(sending_user_id, receiving_user_id, amount))
conn = sqlite3.connect("bot_data.db")
conn = sqlite3.connect(DATABASE_URL)
c = conn.cursor()
with conn:
c.execute("SELECT pending FROM users WHERE user_id = :user_id", {'user_id': sending_user_id})
Expand All @@ -155,7 +155,7 @@ def transfer_points(sending_user_id: int, receiving_user_id: int, amount: float)

def start(update, context) -> None:
if not user_exists(update.message.from_user.id): # Check if the user is already in the DB
conn = sqlite3.connect("bot_data.db")
conn = sqlite3.connect(DATABASE_URL)
c = conn.cursor()
with conn:
c.execute("INSERT INTO users (user_id, username, balance, pending, is_admin) VALUES "
Expand Down Expand Up @@ -188,8 +188,7 @@ def start(update, context) -> None:
def add_profile_conversation(update, context) -> int:
query = update.callback_query
registration_link = r"https://freebitco.in/?r=21441719&tag=telegram"
message = '''
First of all, we need to know your Instagram.
message = '''First of all, we need to know your Instagram.
We will verify that other users really followed you before you send 🍪 to them.

⚠️ It is really important that:
Expand All @@ -209,7 +208,7 @@ def add_profile_conversation(update, context) -> int:

def add_profile(update, context) -> None:
if not is_profile_private(instagram_bot=insta_bot, profile_name=update.message.text):
conn = sqlite3.connect("bot_data.db")
conn = sqlite3.connect(DATABASE_URL)
c = conn.cursor()
with conn:
c.execute("UPDATE users SET profile_link = :profile_link WHERE user_id = :user_id",
Expand Down Expand Up @@ -245,7 +244,7 @@ def get_profile_to_follow(update, context) -> None:
def get_profile_to_follow_thread(update, context) -> None:
query = update.callback_query
current_user = query.from_user.id
conn = sqlite3.connect("bot_data.db")
conn = sqlite3.connect(DATABASE_URL)
c = conn.cursor()
with conn:
c.execute("SELECT profile_link, user_id FROM users WHERE (balance >= -1 AND user_id != :user_id) "
Expand Down Expand Up @@ -298,185 +297,4 @@ def followed_thread(update, context) -> None:
transfer_points(sending_user_id=int(context.user_data['followee_id']),
receiving_user_id=context.user_data['follower_id'], amount=1)
try:
context.bot.send_message(chat_id=context.user_data['followee_id'], text="You just gained a new follower!")
except Exception:
logging.error("Couldn't send follower confirmation to chat id: {}".format(context.user_data['followee_id']))
get_profile_to_follow(update, context)


def skip(update, context) -> None:
balance_to_pending(context.user_data['followee_id'], -1)
add_action(int(context.user_data['follower_id']), int(context.user_data['followee_id']), 'skipped')
get_profile_to_follow(update, context)


def end_session_job(context) -> None:
job = context.job
context.bot.delete_message(chat_id=job.context.chat_id, message_id=job.context.message_id)


def add_points(update, context) -> None:
if is_admin(update.message.from_user.id):
output = ""
if len(context.args) == 2:
user_id = int(context.args[0])
if user_exists(user_id=user_id):
conn = sqlite3.connect("bot_data.db")
c = conn.cursor()
with conn:
c.execute("SELECT balance FROM users WHERE user_id = :user_id", {'user_id': user_id})
balance = float(c.fetchone()[0])
balance += int(context.args[1])
c.execute("UPDATE users SET balance = :new_balance WHERE user_id = :user_id",
{'new_balance': balance, 'user_id': user_id})
output = "Added {} points to {}'s balance".format(int(context.args[1]), user_id)
else:
output = "Error: no such user in the DB."
else:
output = "Error: this command takes 2 arguments. /add_points USER_ID AMOUNT"
context.bot.send_message(chat_id=update.message.chat_id, text=output)


def get_all_users(update, context) -> None:
if is_admin(update.message.from_user.id):
output = ""
if len(context.args) == 0:
conn = sqlite3.connect("bot_data.db")
c = conn.cursor()
with conn:
c.execute("SELECT * FROM users")
for user in c.fetchall():
output += "ID: {}, Username: {}, Profile: {}, Balance: {}, Pending {}\n".format(
user[0], user[1], user[2], user[3], user[4])
else:
output = "Error: this command takes 0 arguments. /get_users"
if output == "":
output = "No users to display"
context.bot.send_message(chat_id=update.message.chat_id, text=output)


def set_something(update, context) -> None:
conn = sqlite3.connect("bot_data.db")
c = conn.cursor()
amount = float(context.args[2])
what = context.args[1]
user = context.args[0]
with conn:
c.execute("UPDATE users SET "+what+" = :amount WHERE user_id = :user_id",
{'amount': amount, 'user_id': user})


def get_balance(update, context) -> None:
output = ""
conn = sqlite3.connect("bot_data.db")
c = conn.cursor()
with conn:
c.execute("SELECT * FROM users WHERE user_id = :user_id", {'user_id': update.message.from_user.id})
user = c.fetchone()
if len(user) > 0:
output = "ID: {}, Username: {}, Profile: {}, Balance: {}\n".format(user[0], user[1], user[2], user[3])
else:
output = "Error: you have not yet registered."
context.bot.send_message(chat_id=update.message.chat_id, text=output)


def get_all_actions(update, context) -> None:
if is_admin(update.message.from_user.id):
output = ""
if len(context.args) == 0:
conn = sqlite3.connect("bot_data.db")
c = conn.cursor()
with conn:
c.execute("SELECT * FROM follows")
for action in c.fetchall():
output += "ID: {}, Follower: {}-{}, Followee: {}-{}, Status: {}, Date {}\n".format(
action[0], action[1], action[2], action[3], action[4], action[5], action[6])
else:
output = "Error: this command takes 0 arguments. /get_actions"
if output == "":
output = "No actions to display"
context.bot.send_message(chat_id=update.message.chat_id, text=output)


def update_points(update, context) -> None:
if is_admin(update.message.from_user.id):
conn = sqlite3.connect("bot_data.db")
c = conn.cursor()
with conn:
c.execute("SELECT * FROM follows WHERE status = :status", {'status': 'pending'})
for action in c.fetchall():
thread = threading.Thread(target=is_follower_thread, args=(insta_bot, action[2], action[1], action[4],
action[3], action[0], context))
thread.start()


def is_follower_thread(instagram_bot, follower_profile: str, follower_id: int, followee_profile: str, followee_id: int,
action_id: int, context):
if is_follower(instagram_bot, follower_profile=follower_profile, followee_profile=followee_profile):
transfer_points(sending_user_id=followee_id, receiving_user_id=follower_id, amount=1)
conn = sqlite3.connect("bot_data.db")
c = conn.cursor()
with conn:
c.execute("UPDATE follows SET status = :status WHERE action_id = :action_id", {'status': 'approved',
'action_id': action_id})
try:
context.bot.send_message(chat_id=followee_id, text="You just gained a new follower!")
except Exception:
logging.error("Couldn't send follower confirmation to chat id: {}".format(followee_id))


def main() -> None:
global insta_bot
load_settings()
initiate_db()
logging.basicConfig(format='[%(asctime)s] - %(message)s', datefmt='%d-%b-%y %H:%M:%S',
level=logging.INFO) # initialize logging module and format. exclude debug messages
insta_bot = Client()
succ = False
relogin = True
try:
succ = insta_bot.login(username=settings["insta_username"], password=settings["insta_password"])
print(succ)
except exceptions.SentryBlock:
for i in range(10):
print("Try #" + str(i))
try:
succ = insta_bot.login(username=settings["insta_username"], password=settings["insta_password"],
relogin=relogin)
if succ:
print("Connection established!")
break
except exceptions.SentryBlock:
pass
except exceptions.ReloginAttemptExceeded:
relogin = False
except:
pass
time.sleep(random.randint(3, 5))
updater = Updater(settings['TOKEN'], use_context=True)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('update_points', update_points))
updater.dispatcher.add_handler(CommandHandler('set', set_something))
updater.dispatcher.add_handler(CommandHandler('get_users', get_all_users))
updater.dispatcher.add_handler(CommandHandler('get_actions', get_all_actions))
updater.dispatcher.add_handler(CommandHandler('add_points', add_points))
updater.dispatcher.add_handler(CommandHandler('balance', get_balance))
updater.dispatcher.add_handler(CallbackQueryHandler(get_profile_to_follow, pattern='start_following'))
updater.dispatcher.add_handler(CallbackQueryHandler(followed, pattern='confirm_follow'))
updater.dispatcher.add_handler(CallbackQueryHandler(skip, pattern='skip'))
add_profile_conv_handler = ConversationHandler(
entry_points=[CallbackQueryHandler(add_profile_conversation, pattern='set_profile_link')],
states={
ADDRESS: [MessageHandler(Filters.text & ~Filters.command, add_profile)],
}, fallbacks=[CommandHandler('cancel', cancel), CallbackQueryHandler(cancel, pattern='cancel')], )
updater.dispatcher.add_handler(add_profile_conv_handler)
updater.start_polling()
updater.idle()


if __name__ == '__main__':
global settings
global insta_bot
ADDRESS = range(4)
SESSION_TIME = 5 # in seconds
main()
...