如何解决Telethon - 为什么我不能在 API 调用中将用户添加到频道?
我正在 Django 项目中使用 Telethon API,我想通过 API 调用以编程方式将用户添加到某个频道。到目前为止,我有这个:
@csrf_exempt
@api_view(['GET'])
def add_telegram_user_to_channel(request,*args,**kwargs):
channel_id = str(kwargs.get('channel_id')).strip()
user_id = str(kwargs.get('user_id')).strip()
status = 'OK'
error = ''
result = ''
user_info = ''
channel_info = ''
try:
user = client.get_entity(user_id)
channel = client.get_entity(channel_id)
result = client(InvitetochannelRequest(
channel,[user]
))
except:
error = traceback.format_exc()
status = 'ERROR'
return JsonResponse({"status": status,"result": result,"error": error})
然而,它似乎不起作用。然后我尝试预先进行等待,就像我在之前的电视测试中所做的那样:
@csrf_exempt
@api_view(['GET'])
async def add_telegram_user_to_channel(request,**kwargs):
channel_id = str(kwargs.get('channel_id')).strip()
user_id = str(kwargs.get('user_id')).strip()
status = 'OK'
error = ''
result = ''
user_info = ''
channel_info = ''
try:
user = await client.get_entity(user_id)
channel = await client.get_entity(channel_id)
result = client(InvitetochannelRequest(
channel,"error": error})
这显然可以运行,但我收到一条错误消息,说我的方法应该返回一个 HttpResponse,而是返回一个协程。任何有关我应该如何处理这个问题或为什么这种尝试错误的帮助将不胜感激。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。