我有一个电报群,当人们加入时,我想给他们发消息。我不是指在群组中发布的消息,我指的是将通过机器人发送给该人的私人消息。我为此找到了一个示例代码,如下所示,但此代码仅适用于成员很少且我为试用目的而设置的组。它在我的 1k 成员组中不起作用。
import requests
import json
import time
BOT_API_KEY = 'xxxxxxxxxxxxxxxxxxx'
CHAT_ID = 'xxxxxxx'
def send_welcome_message(new_member):
# Get the new member's name and ID.
name = new_member['first_name']
user_id = new_member['id']
message = f"Hello {name}, Welcome to Group. "
# Send the message to the new member's private chat.
url = f"https://api.telegram.org/bot{BOT_API_KEY}/sendmessage"
data = {"chat_id": user_id, "text": message}
response = requests.post(url, data=data)
def handle_updates(updates):
for update in updates["result"]:
if "message" in update and "new_chat_members" in update["message"]:
# Get the list of new members.
new_members = update["message"]["new_chat_members"]
for new_member in new_members:
# Send a welcome message for the new member.
send_welcome_message(new_member)
def get_updates(offset=None):
url = f"https://api.telegram.org/bot{BOT_API_KEY}/getupdates"
params = {"timeout": 30, "offset": offset}
response = requests.get(url, params=params)
return json.loads(response.content)
def main():
last_update_id = None
while True:
updates = get_updates(last_update_id)
if len(updates["result"]) > 0:
handle_updates(updates)
last_update_id = updates["result"][-1]["update_id"] + 1
if __name__ == '__main__':
main()
会是什么原因?