Channel-Poll-Bot/bot.py

64 lines
1.9 KiB
Python

#!/usr/bin/env python3
# coding=utf-8
import logging
import config
import sqlite3
from telegram.ext import Updater, MessageHandler, Filters
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - \
%(message)s', level=logging.INFO)
def get_callback_id(db):
cursor = db.cursor()
query = ('SELECT max(callback) FROM message_ids')
cursor.execute(query)
return int(cursor.fetchone()[0]) + 1
def add_message_id(db, message_id, callback):
cursor = db.cursor()
query = ("INSERT INTO message_ids(callback, message_id) values(?,?)")
cursor.execute(query, (callback, message_id))
db.commit()
def add_vote(db, callback_id, user=None):
user_id = user.id
name = str(user.first_name)
if user.last_name:
name += " " + str(user.last_name)
cursor = db.cursor()
query = ("INSERT INTO poll(callback, user_id, name) values(?,?,?)")
cursor.execute(query, (callback_id, user_id, name))
db.commit()
def post(bot, update, db):
chat_id = update.message.chat_id
if chat_id in config.allowed_users:
callback_id = get_callback_id(db)
photo = update.message.photo[0]
file_id = photo.file_id
keyboard = InlineKeyboardMarkup([[InlineKeyboardButton(text="❤️",
callback_data=callback_id)]])
reply = bot.send_photo(chat_id=config.channel_id, photo=file_id,
reply_markup=keyboard)
add_message_id(db, reply.message_id, callback_id)
else:
bot.send_message(chat_id=chat_id, text="Not Authorized")
updater = Updater(token=config.api_key)
dispatcher = updater.dispatcher
db = sqlite3.connect("chanbot.sqlite", check_same_thread=False)
cursor = db.cursor()
dispatcher.add_handler(MessageHandler(Filters.photo, lambda x, y: post(x,
y, db)))
updater.start_polling()