Initial commit

This commit is contained in:
Ed 2023-07-11 22:23:28 +02:00
commit d5ca645470
2 changed files with 65 additions and 0 deletions

44
fz.py Normal file
View File

@ -0,0 +1,44 @@
from telethon import TelegramClient, connection
import logging, asyncio, sqlite3
from telethon.tl.types import MessageActionChatJoinedByLink
from telethon.tl.types import MessageActionChatAddUser
from telethon.tl.types import ReplyInlineMarkup
from telethon.tl.types import KeyboardButtonRow
from telethon.tl.types import KeyboardButton
from telethon import events
from telethon.tl.custom import Button
from asyncio import sleep, Future
from telethon.tl.types import PeerUser, PeerChannel
logging.basicConfig(level=logging.INFO)
api_id = YOUR_API_ID
api_hash = 'YOUR_API_HASH'
db = sqlite3.connect('votes')
client = TelegramClient('bot', api_id, api_hash).start()
verifications = {}
client.max_id = 0
@client.on(events.NewMessage(incoming=True, chats=[PeerUser(user_id=184151234),PeerUser(user_id=52493088)], func=lambda e: e.is_private))
async def handler(ev):
msg = await client.send_message(PeerChannel(channel_id=1340677057), ev.message, buttons=[[Button.inline("👍", f"like"),Button.inline("🌟🌟🌟", b"gold")]])
client.max_id = msg.id
@client.on(events.callbackquery.CallbackQuery)
async def get_vote(ev):
print(ev)
#print(ev.from_id, 'voted for', ev.data)
if ev.data == b"gold":
await ev.answer('Grazie per la tua preferenza! Il tuo oro è stato registrato.\nThanks! The gold has been received owo', alert=True)
db.execute('DELETE FROM votes WHERE is_golden = 1 AND user_id = ?', (ev.original_update.user_id,))
db.execute('INSERT INTO votes(user_id, is_golden, msg_id) VALUES (?,?,?)', (ev.original_update.user_id, 1, ev.original_update.msg_id))
elif ev.data == b"like":
await ev.answer('Grazie per il like!\nThanks for the like!', alert=False)
db.execute('INSERT INTO votes(user_id, is_golden, msg_id) VALUES (?,?,?) ON CONFLICT DO NOTHING', (ev.original_update.user_id, 0, ev.original_update.msg_id))
db.commit()
client.flood_sleep_threshold = 24*60*60
client.run_until_disconnected()

21
readme.md Normal file
View File

@ -0,0 +1,21 @@
# Karaoke Telegram Bot
This simple bot was used to count the number of votes of every attendee during the "Karaoke of Furizon" event.
## Requirements
- telethon
- Your own Telegram API_ID and API_HASH
- A channel where to send the posts for voting
## How does it work?
The bot will listen for private messages from the users defined in the source code. When receiving a message with a picture, it will get forwarded to the desired channel, along with a "like" and a "golden" button, the interactions of with will be recorded and stored in the sqlite database over at "votes.db" with this schema:
```CREATE TABLE "votes" (
"user_id" INTEGER NOT NULL,
"is_golden" INTEGER NOT NULL,
"msg_id" INTEGER NOT NULL,
PRIMARY KEY("user_id","is_golden","msg_id")
)```
From there, it is trivial to open the database and count the votes for each person and decide who's the winner!