Skip to content

Commit

Permalink
added admin broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
LordGhostX committed Sep 23, 2020
1 parent 75c5322 commit d66c378
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion animehive.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json
import datetime
import threading
from multiprocessing import Pool
import pymongo
import telegram
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext import CallbackQueryHandler
Expand All @@ -10,6 +12,7 @@
from extras import *

config = json.load(open("config.json"))
bot = telegram.Bot(token=config["token"])
updater = Updater(token=config["token"], use_context=True)
dispatcher = updater.dispatcher
client = pymongo.MongoClient(config["db"]["host"], config["db"]["port"])
Expand Down Expand Up @@ -73,12 +76,27 @@ def send_download_search(anime_list, chat_id, context):
pass


def launch_broadcast(args):
try:
bot.send_message(chat_id=args[0], text=args[1])
except:
pass


def send_broadcast(message, sender):
users = [[i["chat_id"], message] for i in db.users.find({})]
with Pool(5) as p:
result = p.map(launch_broadcast, users)
bot.send_message(
chat_id=sender, text="Finished sending broadcast message to users")


def start(update, context):
chat_id = update.effective_chat.id
first_name = update["message"]["chat"]["first_name"]
if not db.users.find_one({"chat_id": chat_id}):
db.users.insert_one(
{"chat_id": chat_id, "last_command": None, "date": datetime.datetime.now()})
{"chat_id": chat_id, "last_command": None, "admin": False, "date": datetime.datetime.now()})
context.bot.send_message(
chat_id=chat_id, text=config["messages"]["start"].format(first_name))
context.bot.send_message(chat_id=chat_id, text=config["messages"]["menu"])
Expand Down Expand Up @@ -124,6 +142,16 @@ def get_info(update, context):
"$set": {"last_command": "get_info"}})


def broadcast(update, context):
chat_id = update.effective_chat.id
if db.users.find_one({"chat_id": chat_id}).get("admin"):
num_users = db.users.count_documents({})
context.bot.send_message(
chat_id=chat_id, text=config["messages"]["broadcast"].format(num_users))
db.users.update_one({"chat_id": chat_id}, {
"$set": {"last_command": "broadcast"}})


def echo(update, context):
chat_id = update.effective_chat.id
bot_user = db.users.find_one({"chat_id": chat_id})
Expand Down Expand Up @@ -171,7 +199,15 @@ def echo(update, context):
"Get Anime Info ℹ️", callback_data="i=" + anime["session"])]]
context.bot.send_photo(chat_id=chat_id, photo=anime["poster"], caption=config["messages"]["recommendation_result"].format(
anime["title"], anime["status"], "{} {}".format(anime["season"], anime["year"])), reply_markup=InlineKeyboardMarkup(markup))
elif last_command == "broadcast":
if bot_user.get("admin"):
message = update.message.text
thread = threading.Thread(
target=send_broadcast, args=[message, chat_id])
thread.start()
else:
if bot_user.get("admin"):
context.bot.send_message(chat_id=chat_id, text=update.message.text)
context.bot.send_message(
chat_id=chat_id, text=config["messages"]["unknown"])
db.users.update_one({"chat_id": chat_id}, {"$set": {"last_command": None}})
Expand Down Expand Up @@ -236,6 +272,8 @@ def button(update, context):
dispatcher.add_handler(download_handler)
get_info_handler = CommandHandler("info", get_info)
dispatcher.add_handler(get_info_handler)
broadcast_handler = CommandHandler("broadcast", broadcast)
dispatcher.add_handler(broadcast_handler)
echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
dispatcher.add_handler(echo_handler)
button_handler = CallbackQueryHandler(button)
Expand Down

0 comments on commit d66c378

Please sign in to comment.