替换电报上的功能
问题描述:
我试图学习如何在电报上使用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'])
答
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'])
它现在的工作!谢谢你@SatanDmytro – Sile