/
bot-telegram2.py
81 lines (58 loc) · 2.51 KB
/
bot-telegram2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Basic example for a bot that uses inline keyboards.
# This program is dedicated to the public domain under the CC0 license.
"""
import logging
import telegram
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, MessageHandler, Filters
TOKEN = '460707674:AAE5OGTYTjE0m27Uy9yiscSp3B0eiRF5s4M'
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def listener(mensajes):
for m in mensajes:
chat_id = m.chat.id
texto = m.text
print('ID: ' + str(chat_id) + ' - MENSAJE: ' + texto)
def start(bot, update):
keyboard = [[InlineKeyboardButton("Option 1", callback_data='1'),
InlineKeyboardButton("Option 2", callback_data='2')],
[InlineKeyboardButton("Option 3", callback_data='3')]]
reply_markup = InlineKeyboardMarkup(keyboard)
#print(update)
update.message.reply_text('Please choose:', reply_markup=reply_markup)
def button(bot, update):
query = update.callback_query
bot.edit_message_text(text="Selected option: {}".format(query.data),
chat_id=query.message.chat_id,
message_id=query.message.message_id)
def help(bot, update):
update.message.reply_text("Use /start to test this bot.")
def other(bot, update):
#print(bot)
#print(update.message.text)
bot.send_message(chat_id=update.message.chat_id, text=update.message.text)
update.message.reply_text("Other")
def error(bot, update, error):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, error)
def main():
updater = Updater(TOKEN)
bot = telegram.Bot(TOKEN)
print(bot.get_me())
# Create the Updater and pass it your bot's token.
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
updater.dispatcher.add_handler(CommandHandler('help', help))
updater.dispatcher.add_handler(MessageHandler(Filters.text, other))
updater.dispatcher.add_handler(MessageHandler(Filters.command, other))
updater.dispatcher.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT
updater.idle()
if __name__ == '__main__':
main()