I'm try to make a very simple telegram bot using telepot and a webhook. The bot works quite well but it doesn't recognize messages sent before the obtaining of the necessary permits for reading messages. Here's the code:
TOKEN = '*****'
secret = '****'
bot = telepot.Bot(TOKEN)
bot.setWebhook("https://***.pythonanywhere.com/{}".format(secret), max_connections=1)
app = Flask(__name__)
@app.route('/{}'.format(secret), methods=["POST"])
def telegram_webhook():
update = request.get_json()
if "message" in update:
msg = update["message"]
chat_id = msg["chat"]["id"]
if msg["chat"]["type"] == "private":
******
elif msg["chat"]["type"] == "group":
if "text" in msg:
text = msg["text"]
user_bot = bot.getMe()
******
elif text.startswith('/kick'):
admins = bot.getChatAdministrators(chat_id)
admins_id = [chatmember["user"]["id"] for chatmember in admins]
if msg["from"]["id"] in admins_id:
if "reply_to_message" in msg:
msg = msg["reply_to_message"]
user_id = msg["from"]["id"]
if bot.kickChatMember(chat_id, user_id):
if user_id != user_bot["id"]:
bot.sendMessage(chat_id, "{} was kicked".format(msg["from"]["first_name"]))
else:
if user_id != user_bot["id"]:
bot.sendMessage(chat_id, "Error, the bot must be an admin")
else:
bot.sendMessage(chat_id, "You have to reply to a message", reply_to_message_id =
msg["message_id"])
else:
bot.sendMessage(chat_id, "You must be an admin!", reply_to_message_id = msg["message_id"])
except:
pass
return "OK"
For example when I reply to a very old message using "/kick" the bot says "You have to reply to a message"