python - 在 Telepot 上弄脏我的手并面临问题
问题描述
我从这里和那里挑选了一些东西,几个月前它工作得很好,但是最近当我回到我的项目时,代码给出了一个错误,我什至不是 python 的初学者,只是想边学边学.
Traceback (most recent call last):
File "/home/soumyadeep/Downloads/telepot.py", line 2, in
<module>
import telepot, time
File "/home/soumyadeep/Downloads/telepot.py", line 38, in
<module>
bot = telepot.Bot('Token')
AttributeError: partially initialized module 'telepot' has no
attribute 'Bot' (most likely due to a circular import)
是错误,代码是
#!/usr/bin/python
import telepot, time
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
if (content_type == 'text'):
command = msg['text']
print ('Got command: %s' % command)
if '/hello' in command:
bot.sendMessage(chat_id, "Hello, do you have any commands for today?")
if '/iamsudo' in command:
bot.sendMessage(chat_id, "Hello, Soumyadeep. How can I assist you today?")
if '/iamroot' in command:
bot.sendMessage(chat_id, "Hello, Soumyadeep. How can I assist you today?")
if '/NodeRed' in command:
bot.sendMessage(chat_id, "Let me help you with that. Initiating in some time.")
if '/name' in command:
bot.sendMessage(chat_id, "You can call me Ether.")
if '/master' in command:
bot.sendMessage(chat_id, "I am a roughly done bot from Soumyadeep Chatterjee.")
if '/mother' in command:
bot.sendMessage(chat_id, "Requesting initiation.")
if '/start' in command:
bot.sendMessage(chat_id, "Hello. What can I assist you with?")
if '/delta' in command:
bot.sendMessage(chat_id, "Initiating delta sequence. Awaiting authorization from home server.")
if '/goodnight' in command:
bot.sendMessage(chat_id, "Initiating do no disturb throughout the network. ")
if '/goodmorning' in command:
bot.sendMessage(chat_id, "Good morning, love. Refreshing the network.")
if '/goodbye' in command:
bot.sendMessage(chat_id, "Have a good day, love. Starting a lockdown on the home server.")
# Creates a bot using the token provided by BotFather
bot = telepot.Bot('Token')
# Add the handle function to be called every new received
message
bot.message_loop(handle)
# Wait for new messages
while 1:
time.sleep(20)
解决方案
错误消息准确地告诉你你做了什么。你有一个名为的文件telepot.py
正在做import telepot
. 你不能那样做。那是循环导入。如果您有一个名为 的模块telepot
,则必须将程序的文件名更改为其他名称。
顺便说一句,您的一长串if
语句应该是if
/ elif
/ elif
/ elif
...如果消息总是以此开头,那么将所有这些消息放在字典中并基于单个查找进行查找会更聪明在第一个词上。
推荐阅读
- typescript - TypeScript:如何在 try 块中设置 const 变量?
- javascript - 长按图像弹出像jquery中的instagram?
- c - 在 C 中使用 Strtok() 对二维数组进行分段错误
- python - Spyder 无法启动
- javascript - 如何在浏览器中获取未处理的承诺拒绝的来源?
- c - 代码块错误:没有输入文件。C代码
- java - json文件没有被读取
- youtube-api - 搜索结果有几个“nextPageToken”,但没有更多项目
- python-3.x - python PyQT5中带有外来字符的Python正则表达式
- wpf - 停止动画而不重置到开始位置 wpf