Can't respond to messages in message request in fbchat - python

I'm trying to make a bot so that when someone sends it a message the bot sends back a joke but I've noticed this only works with people in my friends list but if random people sent to the bot then their message gets stuck in message request on messenger and even the fbchat library recognizes this and tells me in the console that there is a new pending message so is there a way to respond to those too?
Here is my code:
from fbchat import Client,ThreadLocation
class JokesBot(Client):
def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs):
self.moveThread(ThreadLocation.PENDING,thread_id,thread_type)
self.markAsDelivered(thread_id, message_object.uid)
self.markAsRead(thread_id)
# If you're not the author, echo
if author_id != self.uid:
self.send(Message(text='wiso tefl hehe'), thread_id=thread_id, thread_type=thread_type)
client = JokesBot('email', 'password')
client.listen()
Bare in mind I still didn't implement the jokes so it's missing from the code
Thanks in advance

onMessage event called only when connected facebook friend sends you a message.
for unconnected facebook users(not friends) you should use another event onPendingMessage but unlike onMessage' in pending message things are getting more complex:
take a look at fbchat documentation
after the event called you should call the method: "fetchThreadList" in oreder to see the incoming message

Related

Download public group messages without authentiction - Telethon

Is there, at the moment, any way to download messages from a public group without phone authentication? While using a bot, I get an error:
Exception has occurred: BotMethodInvalidError
The API access for bot users is restricted. The method you tried to invoke cannot be executed as a bot (caused by GetHistoryRequest)
I'd like to automate the process of getting new messages so I'm not able to authenticate every time the session starts.
Is there any workaround?
The bot can't get the message history. Bots work with updates. You can receive new messages while your bot is connected
#bot.on(events.NewMessage)
async def echo_all(event):
print(event.message)

Telegram Bot Api / Python: Trying to send voice message via my telegram bot

so im playing a little bit around with my telegram bot, now im already able to send normal messages and pictures. But now i want to be able to send pre recorded voice messages as an answer. In the documentation it says to use send_voice for this matter and it recommends using the file_id of a file thats already on the telegram servers. So thats what i did i send a voice message to the #RawDataBot and he returned to me the file_id of the voice message.
Problem is: When i try to trigger it i get the following error: telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: wrong file identifier/HTTP URL specified
Any ideas here on what i might be doing wrong? Here is the relevant code:
import telebot
API_KEY = <MY_API_KEY>
bot = telebot.TeleBot(API_KEY)
#start
#bot.message_handler(commands=['start'])
def start (message):
bot.send_message(message.chat.id,Textstart)
#bot.message_handler(commands=['pic'])
def start (photo):
bot.send_photo(photo.chat.id, "https://de.wikipedia.org/wiki/Zeus#/media/Datei:Zeus_Otricoli_Pio-Clementino_Inv257.jpg")
#here is the part where he is supposed to send the voice message if someone types in /audio
#bot.message_handler(commands=['audio'])
def start (voice):
bot.send_voice(voice.chat.id,"AwACAgIAAxkBAAEWjl5i5bjyudWAM9IISKWhE1Gjs5ntQgACLx8AApcNKEv97pVasPhBoCkE",)
bot.polling()
There are multiple reasons, but I think it is because of these:
file_id is unique for each individual bot and can't be transferred from one bot to another.
file_id uniquely identifies a file, but a file can have different valid file_ids even for the same bot.
See here.

aiogram message handler not firing for a message containing media

I am using aiogram 2.11.2 as Python interface to the Telegram API, but I am encountering an issue in the simplest callback: it does activate when the message is text only, but fails whenever any media is attached. This includes photos, videos, audios, stickers and GIFs, with or without caption.
I hope I'm not missing out on something.
import aiogram
class TelegramBot(object):
def __init__(self):
self.bot = aiogram.Bot(token="TOKEN")
self.dispatcher = aiogram.Dispatcher(bot=self.bot)
self.dispatcher.register_message_handler(self.on_msg,)
async def on_msg(self, msg: aiogram.types.Message):
print("Message received in telegram")
Turns out I was really missing out on something. To listen for all kinds of messages, it is sufficient to define content_type as
dispatcher.register_message_handler(self.on_msg, content_type=aiogram.types.ContentType.all())
Thanks to the answerer to my Git issue.

Verify message was sent with Twilio - Python

I have a small script that sends text messages with Twilio using Python.
This is my code:
import os
from twilio.rest import Client
account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
client = Client(account_sid, auth_token)
cell_number = os.environ.get('CELL_PHONE_NUMBER')
text_message = input("Enter a message to send: ")
send_message = client.messages.create(from_=os.environ.get('TWILIO_PHONE_NUMBER'),
to=cell_number,
body=text_message
print(send_message)
And this is the response I get back:
<Twilio.Api.V2010.MessageInstance account_sid=MY_ACCOUNT_SID sid=SMc5e8f335198144b4b3c7f401af511f11>
I was wondering what the best way was to validate that the message was actually sent in code.
I thought of doing something like this:
if send_message:
print("Message sent.")
else:
print("Message not sent.")
And I was just wondering if there was a better way to do that.
I understand that this is a relatively old question and you might have figured out a way to achieve the desired behavior, but just wanted to post my thoughts so that it may help other learners like me in the future.
So basically, twilio provides developers with a functionality of webhooks using which the status of messages can be tracked. You will have to provide an extra parameter while creating the message wherein you will have to pass the callback url pointing to a server which will be logging the status of the sent messages.
When your python script sends a SMS by doing a POST request to twilio server, twilio will send the status of the message to the callback URL as a parameter by making a post or a get request.
This document describes how to achieve the above behavior with the help of example code snippets. In case you used another method to track the status of the messages, let us know so that it will help the community.

How to get user info with bot token

I am using python slack client to connect and send messages with a bot application.
slc = SlackClient("BOT_USER_TOKEN")
out = slc.api_call(method='users.profile.get', user='XXX')
I am getting not_allowed_token_type error in output json. I am able to call:
slc.api_call(method='chat.PostMessage',channel)
Is there a way to get user email, name from slack API. previously I got the username from event messages. but now I am getting only user id so I am looking for a solution to fetch user info using that id.
The reason you get this error message is that users.profile.get does not work with bot tokens.
To mitigate just use the user token that you received along with your bot token when installing your Slack app.

Categories

Resources