英文:
Read message with group name telethon (telegram)
问题
Here's the translated code part you provided:
# 我有10个Telegram群,各有不同的名称。我能够使用以下来自Telethon库的示例代码打印消息。我还需要打印群组名称。
EG:
G10001 Bhuvan测试(群组名称 用户名/电话号码 消息)
#!/usr/bin/env python3
# 一个简单的脚本,用于打印一些消息。
import os
import sys
import time
from telethon import TelegramClient, events, utils
def get_env(name, message, cast=str):
if name in os.environ:
return os.environ[name]
while True:
value = input(message)
try:
return cast(value)
except ValueError as e:
print(e, file=sys.stderr)
time.sleep(1)
session = os.environ.get('TG_SESSION', 'printer')
api_id = get_env('TG_API_ID', '请输入您的API ID:', int)
api_hash = get_env('TG_API_HASH', '请输入您的API哈希:')
proxy = None # https://github.com/Anorov/PySocks
# 创建并启动客户端,以便我们可以进行请求(我们不在这里)
client = TelegramClient(session, api_id, api_hash, proxy=proxy).start()
# `pattern`是一个正则表达式,参见https://docs.python.org/3/library/re.html
# 如果您想更交互式地学习,可以使用https://regexone.com/。
#
# "(?i)"使其不区分大小写,而|分隔"选项"。
@client.on(events.NewMessage(pattern=r''))#pattern=r'(?i).*\b(hello|hi)\b'))
async def handler(event):
sender = await event.get_sender()
#client.get_input_entity(PeerChannel(fwd.from_id))
#channel = await event.get_channel()
#group = event.group()
#group = event.get_group()
#print(group)
#print(utils.get_peer_id(sender))
#print(utils.get_input_location(sender))
#print(utils.get_input_dialog(sender))
#print(utils.get_inner_text(sender))
#print(utils.get_extension(sender))
#print(utils.get_attributes(sender))
#print(utils.get_input_user(sender))
name = utils.get_display_name(sender)
print(name, '说:', event.text, '!')
#print(utils.get_input_entity(PeerChannel(sender)))
#print(utils.get_input_channel(get_input_peer(channel)))
try:
print('(按Ctrl+C停止)')
client.run_until_disconnected()
finally:
client.disconnect()
# 注意:我们使用try/finally来显示可以用这种方式完成,但使用:
#
# with client:
# client.run_until_disconnected()
#
# 几乎总是更好的做法。
# 我还需要帮助发送消息到群组。我已经阅读了下面提供的一些答案,但对我没有用。
#(https://stackoverflow.com/questions/52002524/sending-telegram-messages-with-telethon-some-entity-parameters-work-others-don)
Please let me know if you need any further assistance or additional code translation.
英文:
I have 10 groups in telegram with different name. I am able to print message with the below example code from telethon library. I also need the group name to be printed.
EG:
G10001 Bhuvan Testing (GroupName UserName/Phoneno Message)
#!/usr/bin/env python3
# A simple script to print some messages.
import os
import sys
import time
from telethon import TelegramClient, events, utils
def get_env(name, message, cast=str):
if name in os.environ:
return os.environ[name]
while True:
value = input(message)
try:
return cast(value)
except ValueError as e:
print(e, file=sys.stderr)
time.sleep(1)
session = os.environ.get('TG_SESSION', 'printer')
api_id = get_env('TG_API_ID', 'Enter your API ID: ', int)
api_hash = get_env('TG_API_HASH', 'Enter your API hash: ')
proxy = None # https://github.com/Anorov/PySocks
# Create and start the client so we can make requests (we don't here)
client = TelegramClient(session, api_id, api_hash, proxy=proxy).start()
# `pattern` is a regex, see https://docs.python.org/3/library/re.html
# Use https://regexone.com/ if you want a more interactive way of learning.
#
# "(?i)" makes it case-insensitive, and | separates "options".
@client.on(events.NewMessage(pattern=r''))#pattern=r'(?i).*\b(hello|hi)\b'))
async def handler(event):
sender = await event.get_sender()
#client.get_input_entity(PeerChannel(fwd.from_id))
#channel = await event.get_channel()
#group = event.group()
#group = event.get_group()
#print(group)
#print(utils.get_peer_id(sender))
#print(utils.get_input_location(sender))
#print(utils.get_input_dialog(sender))
#print(utils.get_inner_text(sender))
#print(utils.get_extension(sender))
#print(utils.get_attributes(sender))
#print(utils.get_input_user(sender))
name = utils.get_display_name(sender)
print(name, 'said', event.text, '!')
#print(utils.get_input_entity(PeerChannel(sender)))
#print(utils.get_input_channel(get_input_peer(channel)))
try:
print('(Press Ctrl+C to stop this)')
client.run_until_disconnected()
finally:
client.disconnect()
# Note: We used try/finally to show it can be done this way, but using:
#
# with client:
# client.run_until_disconnected()
#
# is almost always a better idea.
I also need help to send message to groups. I have gone through some answer given below, which doesn't working for me.
答案1
得分: 2
First get the chat from incoming event:
chat = await event.get_chat()
Print group name:
try:
if chat.title:
print(chat.title)
except AttributeError:
print('no such attribute present')
Send message to group:
await client.send_message(entity=chat.id, message='hi')
英文:
First get the chat from incoming event:
chat = await event.get_chat()
Print group name:
try:
if chat.title:
print(chat.title)
except AttributeError:
print('no such attribute present')
Send message to group:
await client.send_message(entity=chat.id,message='hi')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论