I created a Telegram bot in Python (3.7) to retrieve articles from a website. I used the start_polling() method from the Python telegram bot library to get commands from the user, but when I run the file in my command line (Windows OS), there is no response at all. The file keeps running and doesn't terminate, and any messages sent to the bot aren't responded to either. The code snippet is given below.
YOUR_TOKEN = secret!
WELCOME = 'Welcome!'
def brain_pickings():
final_reply = pickings()
bot.sendMessage(text=final_reply,parse_mode='html')
updater = Updater(token=YOUR_TOKEN)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', my_start))
dispatcher.add_handler(CommandHandler('brainpickings', brain_pickings))
updater.start_polling()
print('it is happening!')
updater.idle()
print('it is idle!')
The bot does not respond to /start or /brainpickings. The output on the command line (where I had entered the command to run the file):
C:\Users\ANJALI\.vscode\telegbot>python main.py
it is happening!
PS - pickings() is a separate function I defined to retrieve the articles. It runs perfectly fine on its own. Please let me know if it needs to be added here.
I got the bot to run. I tried logging as was suggested in the comment above, and there was some error with the way I had imported my modules. The code is fine otherwise.
Related
As mentioned in the title I am trying to send a message to a specific channel using a discord bot that I've built using Discord.js and Node.js. I am using the following code to send the message to the specific channel: message.guild.channels.cache.find('CHANNEL_ID').send(someEmbed);. The problem is, whenever I add this piece of code to the file and run it, I get an error message in the console. The error is message is the following: if (fn(val, key, this)) TypeError: fn is not a function, this error message refers to a file that is automatically generated. So my question is if I'm doing something wrong or if it's some sort of bug.
You could also try
message.guild.channels.cache.get("CHANNEL_ID").send(someEmbed);
although your code should run without any problems.
I'm trying to make my first Discord bot following this tutorial, but I got the following error:
RuntimeError: SSL is not supported.
At the moment, my code looks like this:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
client = discord.Client()
#client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
client.run(TOKEN)
This code is directly copied and pasted from the tutorial because I wanted to make sure I didn't have a typo that was causing the error. I'm guessing the error is not in that code itself, but in the way something is set up on my computer, but I have no idea how SSL works and where to even start trying to fix it. I tried using the block of code from the answer to this question where someone got the same error but in a different context, but that did not work. Thanks for any help!
You have to run the configuration with pythonw.exe not python.exe. So just edit the configuration and change the python interpreter to pythonw.exe.
SITUATION:
So i followed a 3 video short tutorial by sentdex on youtube named "Alexa Skills w/ Python and Flask-Ask" parts 1, 2 and 3. Basically when i run this skill, alexa will read me the first 10 headlines from reddit.com/ r/ worldnews (cant give more than 8 URL for this post unfortunately).
ERROR I'M HAVING:
I followed all steps and i keep getting this error when i test it out in the Amazon Alexa development site saying: "There was a problem with the requested skill's response". One issue i have is that the alexa development console has been updated a few months ago and is completely different so i have no clue if i did something wrong or not. All the youtube videos i have seen are on the old version which has a different way of doing things. I'm going to outline exactly what i have done and hopefully u guys could point what i did wrong.
WHAT I TRIED:
I will also like to mention that i have tried replacing the contents of the get_headlines function with a return command that returns a string for alexa to say like: "it works". But i got the same error message on the development site. So I'm guessing that my code is fine but i might have configured the setting in my alexa dev account wrong. Below, i have included pictures of every step i have done for this simple program.
EXACTLY STEPS I HAVE TAKEN:
1) I have installed flask, flask-ask, and unidecode with the pip installer
2) I downloaded ngrok to host my site
3) CODE: This is the code i have ran (took out my reddit username and password for obvious reasons). It has no errors and the homepage runs fine. So i guess there is no issue with the code itself.
from flask import Flask, render_template
from flask_ask import Ask, statement, question, session
import json
import requests
import time
import unidecode
app = Flask(__name__)
ask = Ask(app, "/big_reader")
def get_headlines(): # DESCRIPTION: get_headlines function will grab the headlines from redit and then its going "stringify" all the headlines together
# 1) LOG INTO REDDIT API
user_pass_dict = {
'user': 'ENTER_YOUR_REDDIT_USERNAME',#'ENTER_YOUR_USERNAME',
'passwd': 'YOUR_REDDIT_PASSWORD',
'api_type': 'json'
}
# Requesting a session from api
sess = requests.Session()
sess.headers.update( {'User-Agent': 'I am testing Alexa Here'} )
sess.post('https://www.reddit.com/api/login', data=user_pass_dict)
time.sleep(1)
url = 'https://reddit.com/r/worldnews/.json?limit=10'
html = sess.get(url)
data = json.loads(html.content.decode('utf-8'))
titles = []
for listing in data['data']['children']:
titles.append( unidecode.unidecode(listing['data']['title']) )
titles = '...'.join([i for i in titles])
return titles
################################# ALEXA STUFF ###################################################################################################
#app.route('/')
def homepage():
return "This is the Homepage"
# A) ALEXA ASKS SOMETHING:
#ask.launch
def start_skill():
welcome_message = 'Sup, You want some news?'
return question(welcome_message)
# B) MY RESPONSE:
#ask.intent("YesIntent")
def share_headlines():
headlines = get_headlines()
headline_msg = 'The current world news headlines are {}'.format(headlines) #string format the headlines?
return statement(headline_msg)
#ask.intent("NoIntent")
def no_intent():
bye_text = 'bye'
return statement(bye_text)
# RUN
if __name__ == '__main__':
app.run(debug=True)
4) PICTURES OF HOW I SET UP MY ALEXA SKILL: Here are 10 images that shows exactly what my alexa developer web page looks like
https://ibb.co/ZdMdgGF <-- my yes intent
https://ibb.co/4N4JygL <-- the JSON editor screen of my skill
https://ibb.co/c2HDw8h <-- What my interface screen looks like
https://ibb.co/BP6ck2L <--how my ngrok looks after running: ngrok http 5000
https://ibb.co/3k5J7wZ <--copying my ngrok https address to alexa endpoint.
https://imgur.com/H6QGWOo <--i even tried adding "/big_reader" at the end of it.
https://ibb.co/3s3tVQH <--the build was successful
https://ibb.co/wgF7GQ4 <--I tried to start the big reader skill and got error
I had the same problem.
I fixed it by downgrading cryptography to 2.1.4 with pip install cryptography==2.1.4
I'm hosting my own skill backend on a Raspberry Pi, using ngrok to create a tunnel between Amazon and localhost. For me this issue went away when I created and logged into my ngrok account, cut and pasted the ./ngrok authtoken into the Linux command line, and ran the command to create the authorisation token yaml file.
I am trying to figure out if it is possible to trigger a python script to be run using google home? I would like to do something like saying "Hey Google run my python script." And from the Google home would execute running my python script. Anyone know if that is possible?
You could create a Dialogflow app that integrates with the Google Assistant so that when you invoke the app it sends a HTTP request to your server. You could setup your server so that it runs your Python script when you receive that HTTP request.
Tutorial to create a Dialogflow app: https://dialogflow.com/docs/getting-started/building-your-first-agent
I had to do this for my assistant as well. Here's what I did:
First I made a PHP script which wraps my Python script.
The code goes like this:
<?php
$resolved_query = $_POST['resolved_query'];
$keywords=shell_exec('/usr/bin/python <YOUR PYTHON FILE NAME>.py "'.$resolved_query.'"');
echo $keywords;
?>
Next I changed my Default Fallback Intent in dialogflow so that it uses webhook. You can see this option at the bottom of the page in Default Fallback Intent.
Now, in order for you to return the result from Python script, just print the result in your Python script. Like:
print result
Now, if your Python file executes successfully, the result will be stored in $keywords. Just print that out and your response will be back at dialogflow.
This is basically how you can get your Python script to be run by dialogflow.
I'm writing a bot for my discord server and I want to send an automatic direct message to anyone who joins the server for the first time. I have looked through the documentation and I can't find anything about how to do this. I am using python 3.5 and discord.py version 0.16.12.
this should work if you are using the discord py rewrite
make sure you have enabled intents for the bot as shown in this picture
intents=discord.Intents.all()
client = commands.Bot(command_prefix= cmd,intents=intents)
#client.event
async def on_member_join(member):
await member.send(f"your welcome message here")
https://discordpy.readthedocs.io/en/latest/api.html#discord.on_member_join
You can do something along the lines of:
await bot.send_message(member, "hello")