Skip to content

Commit

Permalink
chatbot added
Browse files Browse the repository at this point in the history
  • Loading branch information
noob-kittu committed Nov 23, 2022
1 parent dfe88e3 commit 519617f
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 88 deletions.
42 changes: 42 additions & 0 deletions Yone/Database/chatbot_sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import threading
from sqlalchemy import Column, String
from Yone.Database import BASE, SESSION
class YoneChats(BASE):
__tablename__ = "yone_chats"
chat_id = Column(String(14), primary_key=True)

def __init__(self, chat_id):
self.chat_id = chat_id

YoneChats.__table__.create(checkfirst=True)
INSERTION_LOCK = threading.RLock()


def is_yone(chat_id):
try:
chat = SESSION.query(YoneChats).get(str(chat_id))
return bool(chat)
finally:
SESSION.close()

def set_yone(chat_id):
with INSERTION_LOCK:
yonechat = SESSION.query(YoneChats).get(str(chat_id))
if not yonechat:
yonechat = YoneChats(str(chat_id))
SESSION.add(yonechat)
SESSION.commit()

def rem_yone(chat_id):
with INSERTION_LOCK:
yonechat = SESSION.query(YoneChats).get(str(chat_id))
if yonechat:
SESSION.delete(yonechat)
SESSION.commit()


def get_all_yone_chats():
try:
return SESSION.query(YoneChats.chat_id).all()
finally:
SESSION.close()
87 changes: 0 additions & 87 deletions Yone/Plugins/Admin/anti_channel.py

This file was deleted.

173 changes: 173 additions & 0 deletions Yone/Plugins/User/chatbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import json
import re
import os
import html
import requests
import Yone.Database.chatbot_sql as sql
from korax import Kora

from time import sleep
from telegram.constants import ParseMode
from Yone import dispatcher, SUPPORT_CHAT, KORA_API_TOKEN
from Yone.Plugins.Admin.log_channel import gloggable
from telegram import (CallbackQuery, Chat, MessageEntity, InlineKeyboardButton,
InlineKeyboardMarkup, Message, Update, Bot, User)

from telegram.ext import (CallbackContext, CallbackQueryHandler, CommandHandler,
ApplicationHandlerStop, filters, MessageHandler,
)

from telegram.error import BadRequest, RetryAfter, Forbidden
from telegram.constants import ParseMode

from Yone.Handlers.filters import CustomFilters
from Yone.Handlers.validation import user_admin, user_admin_no_reply

from telegram.helpers import mention_html, mention_markdown, escape_markdown

kora = Kora(KORA_API_TOKEN)
owner = "kittu"
botname = "kora"

@user_admin_no_reply
@gloggable
async def yonerm(update: Update, context: CallbackContext) -> str:
query: Optional[CallbackQuery] = update.callback_query
user: Optional[User] = update.effective_user
match = re.match(r"rm_chat\((.+?)\)", query.data)
if match:
user_id = match.group(1)
chat: Optional[Chat] = update.effective_chat
is_yone = sql.rem_yone(chat.id)
if is_yone:
is_yone = sql.rem_yone(user_id)
return (
f"<b>{html.escape(chat.title)}:</b>\n"
f"AI_DISABLED\n"
f"<b>Admin:</b> {mention_html(user.id, html.escape(user.first_name))}\n"
)
else:
update.effective_message.edit_text(
"Chatbot disable by {}.".format(mention_html(user.id, user.first_name)),
parse_mode=ParseMode.HTML,
)

return ""

@user_admin_no_reply
@gloggable
async def yoneadd(update: Update, context: CallbackContext) -> str:
query: Optional[CallbackQuery] = update.callback_query
user: Optional[User] = update.effective_user
match = re.match(r"add_chat\((.+?)\)", query.data)
if match:
user_id = match.group(1)
chat: Optional[Chat] = update.effective_chat
is_yone = sql.set_yone(chat.id)
if is_yone:
is_yone = sql.set_yone(user_id)
return (
f"<b>{html.escape(chat.title)}:</b>\n"
f"AI_ENABLE\n"
f"<b>Admin:</b> {mention_html(user.id, html.escape(user.first_name))}\n"
)
else:
update.effective_message.edit_text(
"Chatbot enable by {}.".format(mention_html(user.id, user.first_name)),
parse_mode=ParseMode.HTML,
)

return ""

@user_admin
@gloggable
async def yone(update: Update, context: CallbackContext):
user = update.effective_user
message = update.effective_message
msg = f"Choose an option"
keyboard = InlineKeyboardMarkup([[
InlineKeyboardButton(
text="Enable",
callback_data="add_chat({})")],
[
InlineKeyboardButton(
text="Disable",
callback_data="rm_chat({})")]])
await message.reply_text(
msg,
reply_markup=keyboard,
parse_mode=ParseMode.HTML,
)

async def yone_message(context: CallbackContext, message):
reply_message = await message.reply_to_message
if message.text.lower() == "yone":
return True
if reply_message:
if reply_message.from_user.id == context.bot.get_me().id:
return True
else:
return False


async def chatbot(update: Update, context: CallbackContext):
message = update.effective_message
chat_id = update.effective_chat.id
bot = context.bot
is_yone = sql.is_yone(chat_id)
if not is_yone:
return

if message.text and not message.document:
if not yone_message(context, message):
return
Message = message.text
bot.send_chat_action(chat_id, action="typing")
yone = kora.chatbot(message,owner,botname)
sleep(0.3)
await message.reply_text(yone, timeout=60)

async def list_all_chats(update: Update, context: CallbackContext):
chats = sql.get_all_yone_chats()
text = "<b>YONE-Enabled Chats</b>\n"
for chat in chats:
try:
x = context.bot.get_chat(int(*chat))
name = x.title or x.first_name
text += f"• <code>{name}</code>\n"
except (BadRequest, Forbidden):
sql.rem_yone(*chat)
except RetryAfter as e:
sleep(e.retry_after)
await update.effective_message.reply_text(text, parse_mode="HTML")

__help__ = """We have highly artificial intelligence chatbot of telegram which provides you real and attractive experience of chatting.
*Admins only Commands*:
‣ `/Chatbot`*:* Shows chatbot control panel
"""

__mod_name__ = "Chatbot"


CHATBOTK_HANDLER = CommandHandler("ChatBot", yone )
ADD_CHAT_HANDLER = CallbackQueryHandler(yoneadd, pattern=r"add_chat" )
RM_CHAT_HANDLER = CallbackQueryHandler(yonerm, pattern=r"rm_chat" )
CHATBOT_HANDLER = MessageHandler(
filters.TEXT & (filters.Regex(r"^#[^\s]+") & filters.Regex(r"^!")
& filters.Regex(r"^\/")), chatbot )
LIST_ALL_CHATS_HANDLER = CommandHandler(
"allchats", list_all_chats, filters=CustomFilters.dev_filter )

dispatcher.add_handler(ADD_CHAT_HANDLER)
dispatcher.add_handler(CHATBOTK_HANDLER)
dispatcher.add_handler(RM_CHAT_HANDLER)
dispatcher.add_handler(LIST_ALL_CHATS_HANDLER)
dispatcher.add_handler(CHATBOT_HANDLER)

__handlers__ = [
ADD_CHAT_HANDLER,
CHATBOTK_HANDLER,
RM_CHAT_HANDLER,
LIST_ALL_CHATS_HANDLER,
CHATBOT_HANDLER,
]
1 change: 1 addition & 0 deletions Yone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
raise Exception("Please Add Hash Api key to start the bot")

DB_URI = os.environ.get("DATABASE_URL")
KORA_API_TOKEN = os.environ.get("KORA_API_TOKEN")
PHOTO = os.environ.get("PHOTO")
WORKERS = int(os.environ.get("WORKERS", 8))
ALLOW_EXCL = os.environ.get('ALLOW_EXCL', False)
Expand Down
6 changes: 5 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@
"description": "Your Telegram support group chat username where your users will go and bother you with shit But be like: MyGroupChatUsernameBlah. If this ever points to masha support than consider you made an enemy.",
"required": true,
"value": "league_of_bots"
},
},
"KORA_API_TOKEN": {
"description": "generate your kora api token from @Camille_Robot",
"value": ""
},
"PHOTO": {
"description": "Your Telegram Bot Image (Add Link Only).",
"required": true,
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ telegraph
wikipedia
opencv-python--headless
gtts
korax

0 comments on commit 519617f

Please sign in to comment.