forked from andrewome/telegram-quizarium-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.py
67 lines (56 loc) · 1.92 KB
/
Bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import logging
from quizarium.QuizariumGameInstance import QuizariumGameInstance
from store import Store
from dotenv import load_dotenv
from telethon import TelegramClient, sync, events
from telethon.tl.types import PeerUser, PeerChannel, PeerChat
from telethon.tl.custom import Message
# Get constants
load_dotenv()
API = os.getenv("API")
APIHASH = os.getenv("APIHASH")
QUIZARIUM_BOT_ID = 155670507
SESSION_NAME = "bot_session"
# Logging
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', level=logging.WARNING)
# Log in
client = TelegramClient(SESSION_NAME, API, APIHASH)
# Local storage
channels_to_respond = set()
self_id = None
store = Store("./store.json")
store.load()
quizariumInstances = {}
@client.on(events.NewMessage)
async def handler(event):
global self_id, channels_to_respond, store
messageObj = event.message
# Get chat id
peer = messageObj.to_id
if isinstance(peer, PeerChannel):
chat_id = peer.channel_id
if isinstance(peer, PeerUser):
chat_id = peer.user_id
if isinstance(peer, PeerChat):
chat_id = peer.chat_id
# Respond to chat events
msg = messageObj.message
# Get self_id if does not exist
if self_id == None:
client = event.client
user = await client.get_me()
self_id = user.id
# Handle command
if msg == "+respondtochannel":
if event.from_id == self_id:
channels_to_respond.add(chat_id)
await event.reply("Responding to this channel!")
# If from quizarium and inside channels that are given green light to respond to.
if event.from_id == QUIZARIUM_BOT_ID and chat_id in channels_to_respond:
if chat_id not in quizariumInstances:
quizariumInstances[chat_id] = QuizariumGameInstance()
await quizariumInstances[chat_id].parse(msg, store, messageObj)
client.start()
print("Started!")
client.run_until_disconnected()