Untitled
unknown
python
2 years ago
5.3 kB
7
Indexable
Never
import logging import time from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackQueryHandler, ConversationHandler class snailtg: def __init__(self): logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) self.logger = logging.getLogger(__name__) # Define a few command handlers. These usually take the two arguments update and # context. Error handlers also receive the raised TelegramError object in error. def start(self, update, context): """Send a message when the command /start is issued.""" update.message.reply_text('Başladık hadi hayırlısı') def help(self, update, context): """Send a message when the command /help is issued.""" keyboard = [[ InlineKeyboardButton("ADD alert", callback_data="add"), InlineKeyboardButton("DELETE alert", callback_data="delete") ]] reply_markup = InlineKeyboardMarkup(keyboard) update.message.reply_text("choice",reply_markup=reply_markup) return 1 def menu(self,update, context): query = update.callback_query query.answer() choice=query.data if choice == "add": keyboard = [[InlineKeyboardButton("Desert", callback_data="desert")], [InlineKeyboardButton("Mountain", callback_data="mountain")], [InlineKeyboardButton("Beach", callback_data="beach")], [InlineKeyboardButton("Glacier", callback_data="glacier")], [InlineKeyboardButton("Forest", callback_data="forest")], [InlineKeyboardButton("Space", callback_data="space")], [InlineKeyboardButton("Pass", callback_data="pass")]] reply_markup = InlineKeyboardMarkup(keyboard) update.callback_query.edit_message_text("ekleyemm",reply_markup=reply_markup) if choice == "delete": update.callback_query.edit_message_text("silemmmm ") return 2 def add(self,update, context): query = update.callback_query query.answer() choice = query.data if choice == "add": keyboard = [[InlineKeyboardButton("Hot", callback_data="hot")], [InlineKeyboardButton("Cold", callback_data="cold")], [InlineKeyboardButton("Wind", callback_data="wind")], [InlineKeyboardButton("Wet", callback_data="wet")], [InlineKeyboardButton("Snow", callback_data="snow")], [InlineKeyboardButton("Storm", callback_data="storm")], reply_markup = InlineKeyboardMarkup(keyboard) update.callback_query.edit_message_text("ekleyemm", reply_markup=reply_markup) return def echo(self, update, context): """Echo the user message.""" update.message.reply_text(update.message.text) def error(self, update, context): """Log Errors caused by Updates.""" self.logger.warning('Update "%s" caused error "%s"', update, context.error) def main(self): """Start the bot.""" # Create the Updater and pass it your bot's token. # Make sure to set use_context=True to use the new context based callbacks # Post version 12 this will no longer be necessary token = "xxxxxxxx" self.updater = Updater(token, use_context=True) self.updater.start_polling() # Get the dispatcher to register handlers dp = self.updater.dispatcher # on different commands - answer in Telegram dp.add_handler(CommandHandler("start", self.start)) dp.add_handler(CommandHandler("help", self.help)) # on noncommand i.e message - echo the message on Telegram dp.add_handler(MessageHandler(Filters.text, self.echo)) conv_handler = ConversationHandler( entry_points=[CommandHandler('help', self.help)], states={ 1: [CallbackQueryHandler(self.menu)], 2: [CallbackQueryHandler(self.add)] }, fallbacks=[CommandHandler('help', self.help)] ) dp.add_handler(conv_handler) # log all errors dp.add_error_handler(self.error) # Start the Bot self.updater.start_polling(timeout=600) self.myuser_id = '887864956' self.updater.dispatcher.bot.send_message(chat_id=self.myuser_id, text='Ansızın mesaj gönderiyorum!') # Run the bot until you press Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. #self.updater.idle() def send(self, msg, id): self.updater.dispatcher.bot.send_message(chat_id=id, text=msg, timeout=15) if __name__ == '__main__': a=snailtg() a.main()