-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
149 additions
and
93 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from examples.nodes import DB, create_tables | ||
from telegrinder import API, Message, Telegrinder, Token, node | ||
from telegrinder.bot.dispatch import Context | ||
from telegrinder.bot.rules import ABCRule, Markup, Text | ||
from telegrinder.modules import logger | ||
|
||
api = API(token=Token.from_env()) | ||
bot = Telegrinder(api) | ||
logger.set_level("DEBUG") | ||
|
||
|
||
class IsChat(ABCRule): | ||
async def check(self, source: node.Source) -> bool: | ||
return source.chat.id < 0 | ||
|
||
|
||
class IsAdmin(ABCRule): | ||
async def check(self, source: node.Source, db: DB, context: Context) -> bool: | ||
result = await db.execute("select * from admins where telegram_id = ?", (source.from_user.id,)) | ||
context["is_admin"] = True | ||
return bool(await result.fetchone()) | ||
|
||
|
||
async def promote(user_id: int, *, db: DB) -> None: | ||
await db.execute("insert into admins(telegram_id) values (?) on conflict do nothing", (user_id,)) | ||
await db.commit() | ||
|
||
|
||
@bot.on.message(IsChat()) | ||
async def photo_in_chat_handler(message: Message, p: node.Photo): | ||
photo_size = p.sizes[-1] | ||
await message.answer("Photo ratio H/W: {}".format(photo_size.height / photo_size.width)) | ||
|
||
|
||
### Two handlers below require DB node | ||
# DB node is marked with scope = PER_EVENT | ||
# therefore DB node will only be resolved once on event | ||
# If you run this code you will see that connection opening | ||
# and closing is going to happen only once. | ||
|
||
|
||
@bot.on.message() | ||
async def photo_handler( | ||
message: Message, | ||
db: DB, | ||
photo: node.Photo, | ||
) -> None: | ||
await message.answer("Got a photo in private message") | ||
|
||
|
||
@bot.on.message() | ||
async def integer_handler( | ||
message: Message, | ||
db: DB, | ||
i: node.TextInteger, | ||
) -> None: | ||
await message.answer(f"{i} + 3 = {i + 3}") | ||
|
||
|
||
@bot.on.message(IsAdmin(), Text("/op")) | ||
async def add_admin_handler(message: Message, db: DB) -> str | None: | ||
if not message.reply_to_message: | ||
return "Need reply" | ||
await promote(message.reply_to_message.unwrap().from_user.id, db=db) | ||
await message.answer("Done") | ||
|
||
|
||
@bot.on.message(Markup("/getadmin <token>")) | ||
async def getadmin_handler(message: Message, token: str, db: DB) -> str: | ||
print(token) | ||
if token != api.token: | ||
return "Wrong token" | ||
await promote(message.from_user.id, db=db) | ||
return "Done" | ||
|
||
|
||
@bot.on.message(Text("/amiadmin"), IsAdmin().optional()) | ||
async def amiadmin_handler(message: Message, is_admin: bool = False): | ||
await message.answer("You are " + ("not " if not is_admin else "") + "an admin") | ||
|
||
|
||
bot.loop_wrapper.lifespan.on_startup(create_tables()) | ||
bot.run_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters