2017-10-18 64 views
0

我試圖學習如何在電報上使用replace函數。要做到這一點,首先我試圖讓它在重複用戶所說的基本機器人上工作。所以,機器人必須替換用戶消息中的字符,但它不起作用。在這個例子中,我試圖讓我的機器人用消息中的「o」替換每個「我」,但它似乎不起作用。替換電報上的功能

def handle(msg): 
    content_type, chat_type, chat_id = telepot.glance(msg) 
    print(content_type, chat_type, chat_id) 


    if content_type == 'text': 
     msg['text'].replace("i", "o") 
     bot.sendMessage(chat_id, msg['text']) 

回答

0

replace函數返回的結果,請嘗試:

def handle(msg): 
    content_type, chat_type, chat_id = telepot.glance(msg) 
    print(content_type, chat_type, chat_id) 


    if content_type == 'text': 
     msg['text'] = msg['text'].replace("i", "o") 
     bot.sendMessage(chat_id, msg['text']) 
+0

它現在的工作!謝謝你@SatanDmytro – Sile