电报群社区里最烦人的是机器人账号,滥发广告,严重影响社区清新的氛围。
本文讲解,如何通过在电报群里,设置自己的机器人账号,监控群内新加入的账号。
检测到机器人账号,立即自动踢群。
请参考之前的博文,创建机器人账号 Telegram Bot account Telegram Bot API Develop
本节重点研究电报群加入新人的通知消息结构体
更多机器人接收的群消息结构,请看上一篇博文:
Telegram Bot Webhook Messages
// 用户自己进群 { "update_id":67000002, "message":{ "message_id":43, "from":{ "id":520000001, "is_bot":false, "first_name":"Henry", "username": "telegram18888" "language_code":"zh-Hans-CN" }, "chat":{ "id":-1000000000007, "title":"rubyfans", "username":"rubyfans", "type":"supergroup" }, "date":1517464283, "new_chat_participant":{ "id":520000001, "is_bot":false, "first_name":"Henry", "username": "telegram18888" "language_code":"zh-Hans-CN" }, "new_chat_member":{ "id":520000001, "is_bot":false, "first_name":"Henry", "username": "telegram18888" "language_code":"zh-Hans-CN" }, "new_chat_members":[ { "id":520000001, "is_bot":false, "first_name":"Henry", "username": "telegram18888" "language_code":"zh-Hans-CN" } ] }, "bot":{ } }
// 邀请用户进群 { "update_id":67000001, "message":{ "message_id":12, "from":{ "id":4500000001, "is_bot":false, "username": "telegram18888" }, "chat":{ "id":-1000000000007, "title":"rubyfans", "username":"rubyfans", "type":"supergroup" }, "date":1517400266, "new_chat_participant":{ "id":520000001, "is_bot":true, "username": "telegramBot" }, "new_chat_member":{ "id":520000001, "is_bot":true, "username": "telegramBot" }, "new_chat_members":[ { "id":520000001, "is_bot":true, "username": "telegramBot" } ] }, "bot":{ } }
require 'uri' require 'net/http' def kick_chat_member(chat_id, user_id) url = "https://api.telegram.org/bot540000000:aaaaaaaaaaabbbbbbbbbbbbcccccccccccc/kickChatMember" data = {'chat_id': chat_id, 'user_id': user_id} puts "kick bot #{chat_id} #{user_id}" response = Net::HTTP.post URI(url), data.to_json, "Content-Type" => "application/json" puts response.inspect end
Telegram 传奇创始经历 Telegram Bot API Bot 官方 API 文档 Python interface for the Telegram Bot API python-telegram-bot
2018-03-06