-1

I want to make an auto reply message with telethon python. But when I run, it cannot get message and cannot reply my message.

This is my code. Anyone can help me please

def chatbot_response(msg):
ints = predict_class(msg, model)
res = getResponse(ints, intents)
return res

if __name__ == '__main__':
# Create the client and connect
# use sequential_updates=True to respond to messages one at a time
client = TelegramClient(session_file, api_id, api_hash, sequential_updates=True)

@client.on(events.NewMessage(incoming=True))
async def handle_new_message(event):  
    if event.is_private:  # only auto-reply to private chats
        from_ = await event.client.get_entity(event.from_id)  
        if not from_.bot:  # don't auto-reply to bots
            msg = events.NewMessage(incoming=True)
            if msg!='':
                message= cchatbot_response(msg)
                print(time.asctime(), '-', event.message)  # optionally log time and message
                time.sleep(1)  # pause for 1 second to rate-limit automatic replies
                await event.respond(message)


print(time.asctime(), '-', 'Auto-replying...')
client.start(phone, password)
client.run_until_disconnected()
corei54590
  • 9
  • 1
  • 4
  • replace `msg = events.NewMessage(incoming=True)` with `msg = event.text` – painor Jan 19 '20 at 15:11
  • 1
    The original code you pasted doesn't work per se, and some methods are missing. Please provide a complete minimal example to encourage more people to help you. – Lonami Jan 19 '20 at 15:11

1 Answers1

0

I am able to run your code minus these lines

message = cchatbot_response(msg)
print(time.asctime(), '-', event.message)  # optionally log time and message
time.sleep(1)  # pause for 1 second to rate-limit automatic replies

Please check the cchatbot_response function.

Also, I called this line

client.start(phone, password)

like this

client.start()

It prompted me for my phone number and password but didn't mention it again.

Roofi
  • 82
  • 1
  • 3
  • 15